zoukankan      html  css  js  c++  java
  • django 1.11.1 连接MySQL

    一、定义数据库 settings.py搜索   DATABASES

    参考路径:   D:Python27Libsite-packagesdjangoinappappsettings.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:Python27Libsite-packagesdjangoinappappmodels.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:Python27Libsite-packagesdjangoinapp>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:Python27Libsite-packagesdjangoinappappsettings.py on line 135, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

    解决办法

    D:Python27Libsite-packagesdjangoinappappsettings.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:Python27Libsite-packagesdjangoinapp>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.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+mysql的简单开发教程

     

     

    文档

    Multiple databases

  • 相关阅读:
    2.Redis常用命令
    1.CentOS安装Redis
    PAT 1127 ZigZagging on a Tree(30分)
    单链表上的基本操作
    求一个序列中的主元素
    PAT 1018 Public Bike Management(30分)
    PAT 1013 Battle Over Cities
    《算法笔记》题目分类
    PAT 1030 Travel Plan(30分)
    CentOS 安装 NET Core SDK 
  • 原文地址:https://www.cnblogs.com/xxx91hx/p/7995206.html
Copyright © 2011-2022 走看看