python3使用Django框架连接mysql(python3+Django+MySQL+pymysql)
改掉系统默认的sqlite3数据库,
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
'ENGINE': 'django.db.backends.mysql',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'NAME':'djangoDB',
'USER':'root',
'PASSWORD':'123456',
'port':'3306',
}
}
然后启动报错,
ImportError: No module named 'MySQLdb'
据说要装MySQL-python
可是MySQL-python一直无法安装成功。
又报错ImportError: No module named 'ConfigParser'
因为python3中叫做configparser,是小写。
好吧,听说不用MySQL-python可以用pymsql代替,然后就赶紧下载pip install pymysql
然后再启动,还是不行啊,还是ImportError: No module named 'MySQLdb'
百度了下,看这篇文章http://www.jianshu.com/p/82781add8449
加了之后就好了。
这里说下MySQL-python吧。
MySQL-python的解释看官方说法
MySQLdb is an interface to the popular MySQL database server for Python. The design goals are:
- Compliance with Python database API version 2.0 [PEP-0249]
- Thread-safety
- Thread-friendliness (threads will not block each other)
MySQL-3.23 through 5.5 and Python-2.4 through 2.7 are currently supported. Python-3.0 will be supported in a future release. PyPy is supported.
说白了就是连接mysql的python接口,有了他就可以用python来操作数据库了。类似java的JDBC那一套东西把。