Python + Django + SAE series tutorial 12 ----- configure MySQL database _ MySQL

Source: Internet
Author: User
Python + Django + SAE series tutorial 12 ----- configure MySQL database pythonDjango

Because SAE supports Mysql, first we need to configure a local Mysql environment, I find mysql-python-1.2.4b4.win32-py2.7.exe on the Internet, and double-click to install


Select a typical installation


After the installation is complete, the page for configuring the database is displayed:


Then, enter the data administrator password:


Finally, run the service.


This process is not complex. After Mysql is installed, the system starts the database service. because Mysql is controlled by command lines, it is still necessary for me to manage such a lazy person by using a visual tool, I select MySQL-Front.

Find mysql_front_setup.1765185107.exe on the Internet, double-click the installation, and open the software after installation:


At this time, we will see several existing databases:


To develop a program, first, we need to create a database (right-click to create a database) named Mytestdb:


OK, so we have created an empty Database. I am not worried about creating the relationship between the table and the table here. We intend to use the Django model to create:

First, let's modify it.

Setting. py, so that Django can understand our new database:

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.constdb_name = sae.const.MYSQL_DB name = sae.const.MYSQL_USER pwd = sae.const.MYSQL_PASS host = sae.const.MYSQL_HOSTport = 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.}}
The above content does not need to be explained, so you can understand that one is a link in the sae environment, and the other is a local link.

In this case, we need to use Django to generate a model and input it in command line mode:

Python manage. py startapp person

This command does not output anything. it only creates a books directory in the mysite directory. Let's take a look at the contents of this directory:
Person/
_ Init _. py
Models. py
Tests. py
Views. py


This directory contains the model and view of this app.
Use your favorite text editor to view the content of the models. py and views. py files. They are all empty, except that there is an import in models. py. This is the basis of your Django app.

The current directory structure is as follows:


Next we will

Edit models. py of person to define our data model:

Models. py:

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)
The above data model is relatively simple. you can understand it at a glance. Note that the foreign key relationship is established as follows:
classroom = models.ForeignKey(ClassRoom)
To let django know the newly added App, you also need to modify some content in setting. py:
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',)
At this time, we can enter the command line program to check whether our app is correct:

Python manage. py validate

If there is no problem, we can use this model to establish the relationship between tables in the database. run the command line to enter the path we have created before, and enter:

Python manage. py sqlall person


In this way, the SQL statements used to create tables in the data model are automatically generated. we can execute them in mysql-front to automatically create tables in the database:


If we copy and paste the statement, there is a problem here. if the long line breaks, the statement will fail to be executed. let's just modify it:


Open the database and refresh it. are all tables automatically created?


We noticed that Django automatically created an auto-increment primary key!

Now that we are developing in sae, let's take a look at how to create a Mysql database in sae, add a table, find the Mysql service of sae, and click initialize:


Manage Mysql:


Execute the following SQL statement we just copied to create a table:


In this way, we created the same table locally and sae and added the response app. The next step is how to perform the most basic operations on the table.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.