python连接mysql数据库&Django配置MySQL数据库


Python3 MySQL 使用pymysql 需先安装 pip3 install pymysql

一、python连接mysql数据库

import pymysql

# 打开数据库连接//ip 账号 密码
conn = pymysql.connect("*.*.*.*", "******", "******")

# 使用cursor()方法获取操作游标
cursor = conn.cursor()

sql_db = """CREATE DATABASE IF NOT EXISTS hardwareTestDb DEFAULT CHARSET utf8 COLLATE utf8_general_ci"""

cursor.execute(sql_db)

print('创建pythonBD数据库成功')

cursor.close()#先关闭游标

conn.close()#再关闭数据库连接

conn = pymysql.connect("*.*.*.*", "******", "******","hardwareTestDb")

sql_table="""CREATE TABLE IF NOT EXISTS userName(NAME VARCHAR(20),company VARCHAR(20),age INT)"""

cursor = conn.cursor()

cursor.execute(sql_table)

print('创建pythonTable成功')

cursor.close()#先关闭游标

conn.close()#再关闭数据库连接

二、Django配置MySQL数据库

DATABASES = {
 'default': {
  'ENGINE': 'django.db.backends.mysql',  # 数据库引擎
  'NAME': 'django',              # 你要存储数据的库名,事先要创建之
  'USER': 'django',              # 数据库用户名
  'PASSWORD': 'django@123',         # 密码
  'HOST': 'localhost',            # 主机
  'PORT': '3306',               # 数据库使用的端口
 }
}

多个数据库连接

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'hardwareTestDb',
        'USER': '********',
        'PASSWORD': '********',
        'HOST': '**.**.**.**',
        'PORT': '3306',
    },
    'mydb': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
>>> BookClass.newObj.using("mydb").all()

Terminal中执行数据库迁移命令

python manage.py makemigrations
python manage.py migrate

在这里插入图片描述

bug
问:
import MySQLdb as DatabaseModuleNotFoundError: No module named 'MySQLdb'
问:pip install mysqlclient报错

" Command :…venv\include\site\python3.8\mysql-python" failed with error code 1 in C:\Users\Administrator\AppData\Local\Temp\pip-install-q7v_ll1n\mysql-python\”

答:
在站点的 init.py 文件中添加,因PyMySQL需高版本 如==1.0.2 以下自动添加

import pymysql
pymysql.install_as_MySQLdb()

问:
TypeError: __init__() takes 1 positional argument but 5 were given
答:
PyMySQL 版本支持 以下版本测试可以 目前好像报错,不建议分别安装。

pip install PyMySQL==0.10.1

问:
django.core.exceptions.ImproperlyConfigured: mysqlclient X.X.X or newer is required; you have 0.X.X.
答:
版本支持 解决办法:
C:\Python37\Lib\site-packages\django\db\backends\mysql(python安装目录)打开base.py,注释掉以下内容:

if version < (1, 3, 13):

raise ImproperlyConfigured(‘mysqlclient 1.3.13 or newer is required; you have %s.’ % Database.version)

问: pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '10.36.174.100' ([WinError 10061] 由于目标计算机积极拒绝,无法连接。)")
答: 非默认端口需增加端口
source_db_ip = pymysql.connect(host=‘10.36..’, user=‘ip’, passwd=’******’, db=‘ip’, port=6346)

问: pymysql.err.OperationalError: (1054, "Unknown column 'xyn3269655' in 'where clause'")
答: 在这里插入图片描述

三、附mysql

如果是rpm安装的启动一般为:

service mysqld start  #启动mysql
service mysqld stop  #关闭mysql

二进制和源码安装的:

/etc/init.d/mysqld start    #启动mysql
/etc/init.d/mysqld stop      #关闭mysql   或者是/usr/bin/mysqladmin -u root -p shutdown
/etc/init.d/mysqld restart  #重启mysql
service mysql start  #启动mysql
service mysql stop  #关闭mysql
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值