您的位置:首页 > 数据库 > SQL

Django如何连接mysql数据库

2015-10-14 17:08 585 查看
(1)安装MySQLdb类库

 方法一:下载安装http://www.djangoproject.com/r/python-mysql/

 方法二:pip安装  
 sudo pip install mysql-python
 显示Successfully installed mysql-python-1.2.5

(2)修改settings.py 配置数据属性
代码如下:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'mysql', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': 'password',
'HOST': '127.0.0.1', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '3306', # Set to empty string for default.
}
}

修改完后进入项目目录下执行python manage.py shell命令启动交互界面输入一下代码验证数据库配置是否成功。没报错则成功!

 代码如下:

>>> from django.db import connection
>>> cursor = connection.cursor()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Django python mysql web