django 1.4, mysql5.5, mysql_for_python1.2.1
将已创建好的项目由sqlite3移植到mysql。已创建好的django项目,详见
http://blog.csdn.net/ppdouble/article/details/7718594
http://blog.csdn.net/ppdouble/article/details/7673359
一、首先创建数据库
mysql> CREATE DATABASE tdata_mysql
-> CHARACTER SET utf8
-> COLLATE utf8_general_ci;
二、修改项目的数据库配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'tdata_mysql', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'python', # Not used with sqlite3.
'HOST': 'python.centos', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
}
}
注意:NAME只需使用数据库名,python.centos是我的本地主机名
三、使用python的数据库API创建数据表,进入项目所在目录运行python manage.py syncdb
# 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 django_content_type
Creating table django_session
Creating table django_site
Creating table tjob_location
Creating table tjob_job
Creating table django_admin_log
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): yes
Username (leave blank to use 'python'): djroot
E-mail address: djroot@python.centos
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
四、使用python的web管理工具http://127.0.0.1/tdjproj/admin/添加数据后,使用mysql客户端工具登录到数据库查看数据变化
mysql> show tables;
+----------------------------+
| Tables_in_tdata_mysql |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_session |
| django_site |
| tjob_job |
| tjob_location |
+----------------------------+
12 rows in set (0.00 sec)
mysql> select * from tjob_job;
+----+------------+-------------+-----------------+-------------+
| id | pub_date | job_title | job_description | location_id |
+----+------------+-------------+-----------------+-------------+
| 1 | 2012-07-16 | SW_engineer | SW_engineer | 1 |
+----+------------+-------------+-----------------+-------------+
1 row in set (0.00 sec)
mysql> select * from tjob_location;
+----+------+-------+---------+
| id | city | state | country |
+----+------+-------+---------+
| 1 | NY | NY | US |
+----+------+-------+---------+
1 row in set (0.00 sec)