django: db howto - 1

以在 Django 中使用 MySQL 爲例,首先要安裝 MySQL 和 MySQL-python 組件,確保 python 能執行 import MySQLdb。python

MySQL 中建立數據庫: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 中的數據庫設置:linux

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 應用:sql

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 中實驗數據庫操做以下:django

from django.db import models

class Employee(models.Model): name = models.CharField(max_length=20) # map 'name' field to db

同步數據庫:session

[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 中查看同步結果:oracle

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)dom

mysql>socket

可見 Employee 類中的 name 所對應的數據庫字段已經自動生成了。

相關文章
相關標籤/搜索