搭建Django2.0+Python3+MySQL5时同步数据库时报错:
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11.None
解决办法:
找到Python安装路劲下的Python36-32\Lib\site-packages\django\db\backends\mysql\base.py文件
将文件中的如下代码注释
if version < (1, 3, 3):
raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
重新在项目manage.py路劲下执行如下命令即可
python manage.py makemigrations
python manage.py migrate
附:Django配置MySQL数据库方法
一、settings.py文件中修改数据库配置为下面的内容:
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': '3306',
'NAME': 'mysql',
'USER': 'root',
'PASSWORD': 'zwg123456',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
}
}
驱动(ENGINE)、主机地址(HOST)、端口号(PORT)、数据库(NAME)、用户名(NAME)以及登录密码(PASSWORD);
二、在__init_.py文件添加如下配置:
# coding=utf-8
import pymysql
pymysql.install_as_MySQLdb()
因为Django连接MySQL时默认使用MySQLdb驱动,但MySQLdb不支持Python3,因此这里将MySQL驱动设置为pymysql。
三、执行数据迁移
在项目manage.py路劲下执行如下命令即可
python manage.py makemigrations
python manage.py migrate
————————————————
版权声明:本文为CSDN博主「潜行100」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_35304570/article/details/79674449
上面执行完后又有报错
File "C:\Users\Administrator\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'
解决方法:
1.用终端找到上面路径的文件
2.在文件中找到 query = query.decode(error = ‘replace’) 这句代码,然后将decode修改成encode就好了
3.在项目manage.py路劲下重新执行如下命令
python manage.py makemigrations
python manage.py migrate
如何通过python shell 执行python一行一行代码 进行数据表的数据添加
进入到python shell
执行命令 python manage.py shell
引入包
from app1.models import Grades,Students
from django.utils import timezone
from datetime import *
查看Grades下的所有数据
Grades.objects.all()
查看第二个
Grades.objects.get(pk=2)
修改数据
grade1.属性=值
grade1.save()
删除
grade1.delete()
添加数据
创建一个模型类的实例对象
grade1=Grades()
grade1.gname="python04"
grade1.gdata=datetime(year=2019,month=11,day=27)
grade1.ggirlnum=3
grade1.gboynum=50
grade1.save()