Python Django Connection Database
First, install the database
# yum-y install MySQL mysql-devel mysql-server
Second, see if the Python database component is installed
# Rpm-qa | grep Mysql-python
Mysql-python-1.2.3-0.3.c1.1.el6.x86_64
Third, create the database
# mysql-uroot-p
mysql> CREATE database csvt character set UTF8;
Iv. creation of Engineering, application
# django-admin.py Startproject csvt03
# CD csvt03
# django-admin.py Startapp Blog
# vim settings.py (Add app, modify database type connect user and password)
...
DATABASES = {
' Default ': {
' ENGINE ': ' Django.db.backends. MySQL',
' NAME ': 'csvt',
' USER ': 'root',
' PASSWORD ': '123456',
' HOST ': ',
' PORT ': ',
}
}
............
Installed_apps = (
' Django.contrib.auth ',
' Django.contrib.contenttypes ',
' Django.contrib.sessions ',
' Django.contrib.sites ',
' Django.contrib.messages ',
' Django.contrib.staticfiles ',
' blog ',
)
...
V. Create a table
# Cat blog/models.py
From django.db import Models
Class Employee (models. Model):
Name = models. Charfield (MAX_LENGTH=20)
(a Name field is added to the Database CSVT employee table)
Vi. synchronizing the database and writing data to the database
# python manage.py syncdb
Creating tables ...
Creating Table Auth_permission
Creating Table Auth_group_permissions
Creating Table Auth_group
Creating Table Auth_user_user_permissions
Creating Table Auth_user_groups
Creating Table Auth_User
Creating Table Auth_message
Creating Table Django_content_type
Creating Table Django_session
Creating Table Django_site
Creating Table Blog_employee
You just installed Django's auth system, which means you don ' t has any superusers defined.
Would to create one now? (yes/no): Yes
Installing Custom SQL ...
Installing indexes ...
No fixtures found.
Vii. Viewing the database
mysql> use csvt;
Database changed
Mysql> show tables;
+----------------------------+
| TABLES_IN_CSVT |
+----------------------------+
| Auth_group |
| auth_group_permissions |
| Auth_message |
| auth_permission |
| Auth_User |
| auth_user_groups |
| auth_user_user_permissions |
| Blog_employee |
| Django_content_type |
| django_session |
| Django_site |
+----------------------------+
Rows in Set (0.00 sec)
mysql> desc blog_employee;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| ID | Int (11) | NO | PRI | NULL | auto_increment |
| name | varchar | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
This article is from "Meteor Yu" blog, please be sure to keep this source http://8789878.blog.51cto.com/8779878/1850748
Vii. Python Django Connection database