django 1.11.1 连接MySQL
一、定义数据库 settings.py搜索 DATABASES
参考路径: D:\Python27\Lib\site-packages\django\bin\app\app\settings.py
'mysql': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '192.168.1.103',
'POST': '3306',
'NAME': 'aaa', # 数据库名称
'USER': 'root',
'PASSWORD': '123456',
'OPTIONS': {
'init_command': "SET sql_mode ='STRICT_TRANS_TABLES' "
}
}

新建了models.py
参考路径D:\Python27\Lib\site-packages\django\bin\app\app\models.py
引入models 模块
创建user表
# -*- coding:utf-8 -*-
from __future__ import unicode_literals
from django.db import models class user(models.Model):
username = models.CharField(max_length=40)
password = models.CharField(max_length=30)
二、同步数据库 manage.py migrate --database=mysql
先执行python manage.py makemigrations
在执行python manage.py migrate --database=mysql
D:\Python27\Lib\site-packages\django\bin\app>manage.py migrate --database=mysql

实例 创建member表
1)models.py文件里 定义数据表结构
class 数据表名 首字母大写

2) python manage.py makemigrations
先要进入项目目录在执行以上命令

3)python manage.py migrate --database=mysql

然后后数据库管理工具看眼

遇到的问题
1.执行 manage.py migrate --database=mysql 报错 ,
SyntaxError: Non-ASCII character '\xe6' in file D:\Python27\Lib\site-packages\django\bin\app\app\settings.py on line 135, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
解决办法
D:\Python27\Lib\site-packages\django\bin\app\app\settings.py 顶部定义编码格式
# -*- coding: utf-8 -*-

2.同样执行 manage.py migrate --database=mysql 报错
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb.
Did you install mysqlclient or MySQL-python?
解决办法
1)安装 Microsoft Visual C++ 9.0
http://aka.ms/vcpython27
下载地址 https://www.microsoft.com/en-us/download/details.aspx?id=44266
2)安装MySQL-python
pip install mysql-python
这里是通过安装 mysqlclient
D:\Python27\Lib\site-packages\django\bin\app>pip install mysqlclient==1.3.4
Collecting mysqlclient==1.3.4
Downloading mysqlclient-1.3.4-cp27-none-win_amd64.whl (1.0MB)
100% |████████████████████████████████| 1.0MB 412kB/s
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.3.4
提示
pip安装可以选择版本安装
比如 pip install mysql-python == 1.2.4 报错
urllib2.HTTPError: HTTP Error 403: SSL is required
增加版本就变成 pip install mysql-python == 1.2.5 报错

mysql-python下载地址
https://pypi.python.org/pypi/MySQL-python/
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 5.6!
https://dev.mysql.com/downloads/connector/python/
版本号问题
Table 3.1 Connector/Python Version Reference
| Connector/Python Version | MySQL Server Versions | Python Versions | Connector Status |
|---|---|---|---|
| 8.0 (continuation of 2.2) | 8.0, 5.7, 5.6, 5.5 | 3.4 and higher, 2.7 | Developer Milestone |
| 2.2 (continues as 8.0) | 5.7, 5.6, 5.5 | 3.4 and higher, 2.7 | Developer Milestone |
| 2.1 | 5.7, 5.6, 5.5 | 3.4 and higher, 2.7, 2.6 | Recommended version |
| 2.0 | 5.7, 5.6, 5.5 | 3.4 and higher, 2.7, 2.6 | GA, Supported |
| 1.2 | 5.7, 5.6, 5.5 (5.1, 5.0, 4.1) | 3.1 and higher, 2.7, 2.6 | GA, Supported |
https://dev.mysql.com/doc/connector-python/en/connector-python-versions.html
Connector/Python
Connector/Python Coding Examples 连接MySQL实例代码
MySQL Connector/Python Developer Guide Connector/Python 开发者指南
Installing Connector/Python from a Binary Distribution 多平台安装
参考资料
文档
django 1.11.1 连接MySQL的更多相关文章
- django新建项目,连接mysql数据库
安装django,进入Django目录,运行 python setup.py install 在workplace目录下新建一个名为site01的项目: cd workplacedjango-admi ...
- VS2019 开发Django(三)------连接MySQL
导航:VS2019开发Django系列 下班回到家,洗漱完毕,夜已深.关于Django这个系列的博文,我心中的想法就是承接之前的微信小程序的内容,做一个服务端的管理中心,上新菜单,调整价格啊!之类的, ...
- Django 3.0 中连接mysql 8.0,可以不使用pymysql ,升级Mysqlclient即可
python 中,连接mysql一般都推荐用pymysql ,而且在django中,网上的教程都是这么连接mysql的. import pymysql pymysql.install_as_MySQL ...
- Django中的app及mysql数据库篇(ORM操作)
Django常见命令 在Django的使用过程中需要使用命令让Django进行一些操作,例如创建Django项目.启动Django程序.创建新的APP.数据库迁移等. 创建Django项目 一把我们都 ...
- Django---Django连接Mysql数据库
前面介绍了Django平台的数据交互,这些数据都是在本地存放着,修改内容或者重新启动服务,数据就消失了,如果我们把数据存放在数据库中,不就保存了吗? Django数据库 Django中自带的也有数据库 ...
- ava基础MySQL存储过程 Java基础 JDBC连接MySQL数据库
1.MySQL存储过程 1.1.什么是存储过程 带有逻辑的sql语句:带有流程控制语句(if while)等等 的sql语句 1.2.存储过程的特点 1)执行效率非常快,存储过程是数据库的服 ...
- django连接mysql数据库以及建表操作
django连接mysql数据库需要在project同名的目录下面的__init__.py里面加入下面的东西 import pymysql pymysql.install_as_MySQLdb() 找 ...
- Ununtu 15.04 安装MySql(Django连接Mysql)
本文介绍Ubuntu 15.04下安装MySQL ubuntu 15.04安装mysql django项目连接mysql 一.安装数据库 1.sudo apt-get install mysql-se ...
- Django连接MySQL出错
错误一:No module named 'MySQLdb' 原因:python3连接MySQL不能再使用mysqldb,取而代之的是pymysql. 解决方法:在python的MySQL包中,即路径: ...
随机推荐
- 关于在win8系统下用VMware 9.0装系统导致物理机不断重启的解决办法
一.问题描述 前段时间将操作系统换成了Win8,安装上VMware 9.0英文版.然后在VMware中安装centos系统,结果每次到安装系统的时候,物理机系统就会莫名其妙地自动重启,毫无征兆地出现, ...
- pkg-config命令的Makefile.am
举例:通过Makefile调用pkg-config命令. pkg-config - Return metainformation about installed libraries (为了使用lib库 ...
- iOS:针对固定数据源,更好的封装cell
一.介绍 在iOS开发中,tableView非常常用,能将其展示出来,它的数据源必不可少.当然数据源有动态下发的,有固定写死的,这里我只探讨固定写死的情况.对于死数据,我们在项目中经常遇到的场景就是我 ...
- (转)Fur Shader
转自:http://qiankanglai.me/misc/2014/11/15/fur-shader/ 花时间看了下毛发效果,苦于囊中羞涩没能买QuickFur.furFX等插件,最后找到了Fur ...
- Web缓存知识体系
转载自[运维社区]https://www.unixhot.com/page/cache
- Andriod书籍准备
老大说公司准备开发MFC项目,过了一段时间又说开发Andriod,好吧,我现在准备Andriod. 鬼知道过段时间会变成什么. http://pan.baidu.com/share/link?shar ...
- PC端和移动端在前端开发上的一些区别,前端里移动端到底比pc端多哪些知识
(1)———————— 前端里移动端到底比pc端多哪些知识,为啥面试时好多公司都问h5水平如何?我做过几年的web前端开发,就简单谈谈自己的感受吧.首先来看看PC端和移动端在前端开发上的一些区别: ( ...
- [Aaronyang] 写给自己的WPF4.5 笔记12[自定义控件-AyImageButton的过程 2/4]
我的文章一定要做到对读者负责,否则就是失败的文章 --------- www.ayjs.net aaronyang技术分享 博文摘要:点击前往文章正文 学会怎样给用户提供事件接口,本例子 ...
- Vue 温故而知新 props如何双向属性绑定
传送门:https://cn.vuejs.org/v2/guide/components-custom-events.html https://segmentfault.com/q/101000001 ...
- [Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现
[Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现 今天做Android项目的时候要用到图片选择,要实现拍照获取图片和从相册获取图片,并且要求在获取完之后可以裁剪,试了很多方法之 ...