加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 教程 > 正文

配置Django使用MySQL数据库的示例

发布时间:2022-06-19 17:24:56 所属栏目:教程 来源:互联网
导读:Django是由Python驱动的开源模型-视图-控制器(MVC)风格的Web应用程序框架了,下面我们就来介绍这款框架配置Django使用MySQL数据库的例子了. 1、安装mysql(Django 安装略): [root@itchenyi-1 Django-1.3.3]# yum install mysql-server mysql-devel [root@itche
  Django是由Python驱动的开源模型-视图-控制器(MVC)风格的Web应用程序框架了,下面我们就来介绍这款框架配置Django使用MySQL数据库的例子了.
 
  1、安装mysql(Django 安装略):
 
  [root@itchenyi-1 Django-1.3.3]# yum install mysql-server mysql-devel
 
  [root@itchenyi-1 Django-1.3.3]# yum install MySQL-python
 
  2、设置Mysql 数据库及用户:
 
  [root@itchenyi-1 Django-1.3.3]# service mysqld start
 
  [root@itchenyi-1 Django-1.3.3]# mysql -u root -p
 
  Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
 
 
  3、编辑 新建的project 配置文件(settings.py):
 
  [root@itchenyi-1 Django-1.3.3]# vi itchenyi/settings.py
 
  DATABASES = {
 
      'default': {
 
          'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
 
  4、切换到新建的project 创建数据库和表:
 
  [root@itchenyi-1 Django-1.3.3]# cd itchenyi/
 
  [root@itchenyi-1 itchenyi]# 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

 
  6、简单验证:
 
  [root@itchenyi-1 itchenyi]# python manage.py Shell
 
  Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
 
  [gcc 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
 
  Type "help", "copyright", "credits" or "license" for more information.
 
  (InteractiveConsole)
 
  >>> import MySQLdb
 
  >>> db = MySQLdb.connect(user='itchenyi',db='itchenyi_db',passwd='your password'
 
  ,host='localhost')
 
  >>>。
 

(编辑:衡阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读