zoukankan      html  css  js  c++  java
  • Django的MySQL Driver配置

    PEP 249规定了Python的数据库API。MySQL主要有三种API实现:

    • MySQLdb 是Andy Dustman开发的、使用原生代码/C语言绑定的驱动,它已经开发了数十年。
    • mysqlclient 是MySQLdb的一个支持Python3的fork,并且可以无缝替换调MySQLdb。mysqlclient目前是MySQL在Django下的推荐选择。
    • MySQL Connector/Python 是Oracle写的,纯Python实现的客户端库。

    以上所有的驱动都是线程安全的,且提供了连接池。MySQLdb 是唯一一个不支持Python3的。

    如果你使用mysqlclient

    settings.py中的配置如下:

     1 # Database
     2 # https://docs.djangoproject.com/en/1.10/ref/settings/#databases
     3 
     4 DATABASES = {
     5     'default': {
     6         'ENGINE': 'django.db.backends.mysql', #数据库引擎
     7         'NAME': 'test',                       #数据库名
     8         'USER': 'root',                       #用户名
     9         'PASSWORD': 'root',                   #密码
    10         'HOST': '',                           #数据库主机,默认为localhost
    11         'PORT': '',                           #数据库端口,MySQL默认为3306
    12         'OPTIONS': {
    13             'autocommit': True,
    14             'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
    15         },
    16     }
    17 }

    第14行主要是为了防止警告:

     (mysql.W002) MySQL Strict Mode is not set for database connection 'default' 

    然后运行migrate :

     1 Operations to perform:
     2   Apply all migrations: admin, auth, contenttypes, sessions, users
     3 Running migrations:
     4   Applying contenttypes.0001_initial... OK
     5   Applying contenttypes.0002_remove_content_type_name... OK
     6   Applying auth.0001_initial... OK
     7   Applying auth.0002_alter_permission_name_max_length... OK
     8   Applying auth.0003_alter_user_email_max_length... OK
     9   Applying auth.0004_alter_user_username_opts... OK
    10   Applying auth.0005_alter_user_last_login_null... OK
    11 。。。
  • 相关阅读:
    C#通过文件头判断图像格式(摘录)
    devenv.exe 应用程序错误
    LINQ TO SQL中的selectMany(转)
    DragDrop registration did not succeed. (摘录)
    API各函数作用简介(转)
    Linq递归用法(摘录)
    (转)逐步为对象集合构建一个通用的按指定属性排序的方法
    何止 Linq 的 Distinct 不给力(转)
    关于sql日志文件
    DES算法的C#实现
  • 原文地址:https://www.cnblogs.com/xb88/p/8057987.html
Copyright © 2011-2022 走看看