zoukankan      html  css  js  c++  java
  • python3.6下pycharm连接mysql

      由于python3.x里面没有了MysqlDB,所以使用python3.6+django连接不上mysql,会报错 no modul "MysqlDB"。于是就有了一个替代品,叫pymysql。

    1. 安装pymysql:

    1 pip3 install pymysql

    2. 把pymysql模块载入到项目之中(__init__文件里加入):  

    1 import pymysql
    2 pymysql.install_as_MySQLdb()

    3. 到settings里设置数据库:

     1 database_host = '192.168.165.129'
     2 database_port = '3306'
     3 database_user = 'root'
     4 database_password = 'abc123'
     5 
     6 DATABASES = {
     7     'default': {
     8         #'ENGINE': 'django.db.backends.sqlite3',
     9         #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    10         'ENGINE': 'django.db.backends.mysql',
    11         'NAME': 'cmdb_master',
    12         'HOST': database_host,
    13         'PORT': database_port,
    14         'USER': database_user,
    15         'PASSWORD': database_password,
    16         'OPTIONS': {
    17             'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
    18         }
    19 
    20     }
    21 }

    注意: 

      以上数据库配置之前需要通过数据库创建配置里指定的数据库(注意名称是否相同)。

    4. 连接测试

     1 python3 manage.py migrate  #运行命令
     2 
     3 System check identified some issues:
     4 Operations to perform:
     5   Apply all migrations: admin, auth, contenttypes, sessions
     6 Running migrations:
     7   Applying contenttypes.0001_initial... OK
     8   Applying auth.0001_initial... OK
     9   Applying admin.0001_initial... OK
    10   Applying admin.0002_logentry_remove_auto_add... OK
    11   Applying contenttypes.0002_remove_content_type_name... OK
    12   Applying auth.0002_alter_permission_name_max_length... OK
    13   Applying auth.0003_alter_user_email_max_length... OK
    14   Applying auth.0004_alter_user_username_opts... OK
    15   Applying auth.0005_alter_user_last_login_null... OK
    16   Applying auth.0006_require_contenttypes_0002... OK
    17   Applying auth.0007_alter_validators_add_error_messages... OK
    18   Applying auth.0008_alter_user_username_max_length... OK
    19   Applying sessions.0001_initial... OK

     显示以上信息说明mysql数据库连接成功,并且自动创建表。

  • 相关阅读:
    ASP.NET程序中常用的三十三种代码
    uri
    用XML保存和读取图片
    location
    访问相关节点
    onkeyup,onkeydown,onkeypress
    Asp.net中Server.Transfer,Server.Execute和Response.Redirect的区别
    关于window.showModalDialog()返回值的学习心得
    WP7数据绑定
    hdu 1568
  • 原文地址:https://www.cnblogs.com/python-nameless/p/6986984.html
Copyright © 2011-2022 走看看