Home Database Mysql Tutorial Python+Django+SAE系列教程12-----配置MySQL数据库_MySQL

Python+Django+SAE系列教程12-----配置MySQL数据库_MySQL

Jun 01, 2016 pm 01:15 PM
password database administrator

pythonDjango

因为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

ADMINS = (  ('hemeng80', 'hemeng80@126.com'),)MANAGERS = ADMINSfrom os import environdebug = not environ.get("APP_NAME", "") if debug:  #LOCAL  db_name = "MyTestDB"    name = "root"   pwd = "123456"  host = "127.0.0.1"  port = "3306"else:  #SAE    import sae.const    db_name = sae.const.MYSQL_DB        name = sae.const.MYSQL_USER     pwd = sae.const.MYSQL_PASS      host = sae.const.MYSQL_HOST     port = sae.const.MYSQL_PORT     host_s = sae.const.MYSQL_HOST_S DATABASES = {   'default': {        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.      'NAME': db_name,                    # Or path to database file if using sqlite3.        'USER': name,                   # Not used with sqlite3.        'PASSWORD': pwd,                # Not used with sqlite3.        'HOST': host,                   # Set to empty string for localhost. Not used with sqlite3.     'PORT': port,                   # Set to empty string for default. Not used with sqlite3.   }}

Copy after login
上面的内容不需要多解释,你就会能明白,一种是在sae环境下的链接,一种是在本地的链接。

这时我们需要使用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

from django.db import models# Create your models here.class ClassRoom(models.Model):    name = models.CharField(max_length=30)  tutor = models.CharField(max_length=30)class Student(models.Model): name = models.CharField(max_length=30)  sex = models.CharField(max_length=5)    age = models.IntegerField() state_province = models.CharField(max_length=30)    qq = models.IntegerField()  classroom = models.ForeignKey(ClassRoom)

Copy after login
上面的数据模型比较简单,你一看就能明白,需要注意外键关系是这样建立的:

1

classroom = models.ForeignKey(ClassRoom)

Copy after login
为了让django认识我们新添加的App,还需要在setting.py中修改一些内容:

1

INSTALLED_APPS = (  'django.contrib.auth''django.contrib.contenttypes''django.contrib.sessions''django.contrib.sites', 'django.contrib.messages''django.contrib.staticfiles',   'person',   # Uncomment the next line to enable the admin:  # 'django.contrib.admin',   # Uncomment the next line to enable admin documentation:    # 'django.contrib.admindocs',)

Copy after login
这时,我们可以进入命令行程序,来检查一下我们的app是否正确:

python manage.py validate

如果没有问题的话,我们就可以使用这个模型来建立数据库中的表和表的关系了 ,命令行进入我们之前建立好的路径,输入:

python manage.py sqlall person


这样自动生成了,数据模型的建立表的SQl,语句,我们在mysql-front里面执行就可以自动创建数据库中的表了:


如果我们复制粘贴的话,这里面有一点问题,其中语句长的换行了,会执行不通过的,我们修改一下就行了:


在打开数据库,刷新看看,是不是我们表都自动创建了?


我们注意到Django自动创建了一个自增的主键!

既然我们在sae中开发,那么下面我就来看看如何在sae中创建一个Mysql的数据库,添加表的,找到sae的Mysql服务,点击初始化:


然后管理Mysql:


在这个里面执行以下我们刚才复制的sql语句创建表 :


这样我们就在本地和sae中创建了相同的表,并添加了响应的app,下一步就是如何对这个表进行最基本的操作了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem that Windows 11 prompts you to enter the administrator username and password to continue? How to solve the problem that Windows 11 prompts you to enter the administrator username and password to continue? Apr 11, 2024 am 09:10 AM

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) How to set router WiFi password using mobile phone (using mobile phone as tool) Apr 24, 2024 pm 06:04 PM

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

Tutorial on changing wifi password on mobile phone (simple operation) Tutorial on changing wifi password on mobile phone (simple operation) Apr 26, 2024 pm 06:25 PM

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) What is the name of the software that can unlock all wifi passwords (recommended mobile software to get connected wifi passwords with one click) Apr 02, 2024 pm 06:46 PM

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 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 Mar 28, 2024 pm 02:35 PM

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 does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

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) How to easily get the WiFi password of a connected mobile phone (quickly find the WiFi password saved in the mobile phone) May 08, 2024 pm 01:10 PM

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? 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? Jun 25, 2024 pm 04:59 PM

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?

See all articles