django: db howto - 1
2013-10-05 22:48
351 查看
以在 Django 中使用 MySQL 为例,首先要安装 MySQL 和 MySQL-python 组件,确保 python 能执行 import MySQLdb。
MySQL 中创建数据库:
创建工程 csvt03,并修改 csvt03/settings.py 中的数据库设置:
创建应用 blog 并修改 csvt03/settings.py 中的应用设置,加入 blog 应用:
Django 中使用 ORM(对象关系模型)来操作数据库,数据库操作的关键类是:django.db.models.Model。
在 blog/models.py 中实验数据库操作如下:
同步数据库:
在 MySQL 中查看同步结果:
mysql> desc blog_employee;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql>
可见 Employee 类中的 name 所对应的数据库字段已经自动生成了。
MySQL 中创建数据库:
[root@bogon csvt03]# mysql -uroot -p Enter password: mysql> create database csvt default charset utf8; Query OK, 1 row affected (0.01 sec) mysql>
创建工程 csvt03,并修改 csvt03/settings.py 中的数据库设置:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'csvt', # Or path to database file if using sqlite3. # The following settings are not used with sqlite3: 'USER': 'root', 'PASSWORD': 'wdlinux.cn', 'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. 'PORT': '', # Set to empty string for default. } }
创建应用 blog 并修改 csvt03/settings.py 中的应用设置,加入 blog 应用:
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', # Uncomment the next line to enable the admin: # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', )
Django 中使用 ORM(对象关系模型)来操作数据库,数据库操作的关键类是:django.db.models.Model。
在 blog/models.py 中实验数据库操作如下:
from django.db import models class Employee(models.Model): name = models.CharField(max_length=20) # map 'name' field to db
同步数据库:
[root@bogon csvt03]# python manage.py syncdb Creating tables ... Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_groups Creating table auth_user_user_permissions Creating table auth_user 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 have any superusers defined. Would you like to create one now? (yes/no): no Installing custom SQL ... Installing indexes ... Installed 0 object(s) from 0 fixture(s) [root@bogon csvt03]#
在 MySQL 中查看同步结果:
mysql> use csvt; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------------------+ | Tables_in_csvt | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | blog_employee | | django_content_type | | django_session | | django_site | +----------------------------+ 10 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(20) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql>
可见 Employee 类中的 name 所对应的数据库字段已经自动生成了。
相关文章推荐
- django: db howto - 2
- How to store an image file to SQliteDB in android
- django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table
- How to run keystone testcase against the DB backend other than SQLITE
- 基于How To Tango With Django 1.9的重新实践(16)——Rango Exercises
- How to handle Mongoose DB connection interruptions
- how to change the implicit default settings in django 1.6
- (Django) How do I set limit_choices_to another class from where the ManyToManyField points to? - django, django-models - TechQues.com
- How to POST a django form with AJAX & jQuery
- How to store an image file to SQliteDB in android
- How to fix "OLE DB provider is not registered"
- How to generate the complex data regularly to Ministry of Transport of P.R.C by DB Query Analyzer
- 基于How To Tango With Django 1.9的重新实践(17)——Hints
- How to install DBDesigner 4
- How to Extend Django User Model
- how to logon Oracle DB
- How to generate the complex data regularly to Ministry of Transport of P.R.C by DB Query Analyzer
- how to get virtualenv working on DjangoStack?
- 基于How To Tango With Django 1.9的重新实践(18)——JQuery
- How To Create SharePoint 2010 Site Collection In Its Own DB