Python+Django+SAE系列教程12-----配置MySQL数据库_MySQL
Jun 01, 2016 pm 01:15 PMpythonDjango
因为SAE上支持的是Mysql,首先我们要在本地配置一个Mysql的环境 ,我在网上找到MySQL-python-1.2.4b4.win32-py2.7.exe,并双击 安装
选择典型安装
安装结束后,会弹出配置数据库的界面 :
然后输数据管理员密码:
最后是运行服务。
这个过程并不复杂,安装完成Mysql以后,系统会启动数据库服务,由于Mysql是用命令行控制的,想我这样懒的 人还是需要借助一个可视化 工具来管理,我选择MySQL-Front。
在网上找到MySQL_Front_Setup.1765185107.exe,双击安装就可以了,安装完打开软件:
这时我们会看到已经有的几个数据库:
我要开发程序首先,我们要建立一个数据库(右键 新建数据库),起名字为Mytestdb:
OK,这样我们就建立了一个空的数据库了,我先不着急在这里建表和表的关系。我们打算使用Django的模型的方式来建立:
首先我们修改一下
setting.py,好让Django认识我们新建立的数据库:
1 |
|
这时我们需要使用Django,生成一个模型,在命令行模式输入:
python manage.py startapp person
这个命令并没有输出什么,它只在 mysite 的目录里创建了一个 books 目录。 让我们来看看这个目录的内容:
person/
__init__.py
models.py
tests.py
views.py
这个目录包含了这个app的模型和视图。
使用你最喜欢的文本编辑器查看一下 models.py 和 views.py 文件的内容。 它们都是空的,除了 models.py 里有一个 import。这就是你Django app的基础。
目前我们的目录结构是这样的:
接下来我们来
编辑 person的models.py,来定义我们的数据模型:
models.py:
1 |
|
1 |
|
1 |
|
python manage.py validate
如果没有问题的话,我们就可以使用这个模型来建立数据库中的表和表的关系了 ,命令行进入我们之前建立好的路径,输入:
python manage.py sqlall person
这样自动生成了,数据模型的建立表的SQl,语句,我们在mysql-front里面执行就可以自动创建数据库中的表了:
如果我们复制粘贴的话,这里面有一点问题,其中语句长的换行了,会执行不通过的,我们修改一下就行了:
在打开数据库,刷新看看,是不是我们表都自动创建了?
我们注意到Django自动创建了一个自增的主键!
既然我们在sae中开发,那么下面我就来看看如何在sae中创建一个Mysql的数据库,添加表的,找到sae的Mysql服务,点击初始化:
然后管理Mysql:
在这个里面执行以下我们刚才复制的sql语句创建表 :
这样我们就在本地和sae中创建了相同的表,并添加了响应的app,下一步就是如何对这个表进行最基本的操作了。

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to solve the problem that Windows 11 prompts you to enter the administrator username and password to continue?

How to set router WiFi password using mobile phone (using mobile phone as tool)

Tutorial on changing wifi password on mobile phone (simple operation)

What is the name of the software that can unlock all wifi passwords (recommended mobile software to get connected wifi passwords with one click)

How to enter the system if you forget your win10 computer power-on password_What to do if you forget your win10 computer power-on password

How does Go language implement the addition, deletion, modification and query operations of the database?

How to easily get the WiFi password of a connected mobile phone (quickly find the WiFi password saved in the mobile phone)

What should I do if my Win10 password does not meet the password policy requirements? What to do if my computer password does not meet the policy requirements?
