一.写在前面
SQLite毕竟是lite,相较于MySQL还是差了不少的。
看一下我的MySQL版本
Server version: 5.7.23 MySQL Community Server (GPL)
二.配置
首先创建一个库:
mysql> create database django charset = utf8;
设置BD Navigator
填好基本信息后点击测试连接报错如下:
Cannot connect to "没压岁钱".
The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
解决方法:
进入MySQL
mysql> SHOW VARIABLES LIKE '%time_zone%';
mysql> SET GLOBAL time_zone='+8:00';
OK,Connection to “没压岁钱” was successful.
至此,DB Browser配置完成!接下来配置Django了
打开项目文件下的setting.py
找到DB配置区
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
修改为如下:
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'USER': 'root',
'NAME': 'django',
'PASSWORD': '123456',
'HOST': 'localhost',
'PORT': '3306',
}
}
好了?NO,启动服务器时报错如下:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
提示安装mysqlclient,但是据前人经验,此库不好安装,应使用pymysql代替之。
pip install pymysql -i https://pypi.mirrors.ustc.edu.cn/simple
然后就是将pymysql
伪装成mysqlclient
打开项目目录下的__init__.py
添加如下内容:
import pymysql
pymysql.install_as_MySQLdb()
这下应该没问题了吧?server起。。。
报错如下:
File "C:\Users\Michael Jiang\AppData\Local\Programs\Python\Python37\Lib\site-packages\django\db\backends\mysql\base.py", lin
e 36, in <module>
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
还嫌版本低了,直接打开base.py改了这个判断条件得了!
#if version < (1, 3, 13):
# raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
直接给你注释掉!server起。。。
报错如下:
File "C:\Users\Michael Jiang\AppData\Local\Programs\Python\Python37\Lib\site-packages\django\db\backends\mysql\operations.py
", line 146, in last_executed_query
query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'
还得改
query = getattr(cursor, '_executed', None)
if query is not None:
#query = query.decode(errors='replace')
return query
直接返回query 不decode了
最后起。。。OK!
记得还要迁移一下
py manage.py migrate
起。。。。
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
August 12, 2019 - 08:46:11
Django version 2.2.4, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
成功!
三.测试
执行一下昨天的代码看一看是否正常!
去数据库看一下是否正确插入。
mysql> select * from polls_student;
+----+--------+-------+
| id | s_name | s_age |
+----+--------+-------+
| 1 | Tom | 19 |
+----+--------+-------+
1 row in set (0.01 sec)
OK,NICE!