将django项目移植到mysql

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)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值