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 VersionMySQL Server VersionsPython VersionsConnector Status
8.0 (continuation of 2.2)8.0, 5.7, 5.6, 5.53.4 and higher, 2.7Developer Milestone
2.2 (continues as 8.0)5.7, 5.6, 5.53.4 and higher, 2.7Developer Milestone
2.15.7, 5.6, 5.53.4 and higher, 2.7, 2.6Recommended version
2.05.7, 5.6, 5.53.4 and higher, 2.7, 2.6GA, Supported
1.25.7, 5.6, 5.5 (5.1, 5.0, 4.1)3.1 and higher, 2.7, 2.6GA, 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+mysql的简单开发教程

 

 

文档

Multiple databases

 

转载于:https://www.cnblogs.com/xxx91hx/p/7995206.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值