Django如何连接mysql数据库
2015-10-14 17:08
585 查看
(1)安装MySQLdb类库
方法一:下载安装http://www.djangoproject.com/r/python-mysql/
方法二:pip安装
(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()
方法一:下载安装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()
相关文章推荐
- MySQL中的integer 数据类型
- java-WEB中的监听器Lisener
- MySQL存储过程
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- GUI - Web前端开发框架
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- Extjs4.0 最新最全视频教程
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- install and upgrade scrapy
- source命令执行SQL脚本文件
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行