这里就不讲如何安装python配置环境变量了,我本机安装的是python3.7,python开发工具是JetBrains PyCharm Edu 2019.2.1,mysql数据库版本是8.035,虚拟机linux环境。 1.由于python很久没用了,先升级pip库到最新版本:执行命令python.exe -m pip install --upgrade pip

image.png 如上显示更新完成。 2.安装python msyqldb,执行命令:pip install mysql-python,报错如下 image.png 报错如下,原因是windows系统部分c++补丁升级问题,网上建议解决方法是,直接安装轮子,mysqlclient-1.4.6-cp37-cp37m-win_amd64.whl,我这里本机是win10系统。 3.下载安装mysqlclient轮子。如下是下载网址,有各个版本mysqlclient供下载,根据自己需要下载 https://pypi.tuna.tsinghua.edu.cn/simple/mysqlclient/ image.png 将mysqlclient-1.4.6-cp37-cp37m-win_amd64.whl文件复制到python安装目录:C:\Users\liuq\AppData\Local\Programs\Python\Python37\Scripts 打开dos窗口进入该目录执行安装命令: pip install mysqlclient-1.4.6-cp37-cp37m-win_amd64.whl如下图显示则安装成功 image.png 4.打开pe工具新建py文件编辑代码如下表示成功安装mysql库

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb
# 打开数据库连接
#db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB", charset='utf8' ) #本机localhost连接
#db = MySQLdb.connect("192.168.10.1",3306 "testuser", "test", "1Q2w3e_!", charset='utf8' )
# 创建数据库连接(远程连接)
db = MySQLdb.connect(
  host='192.168.10.1',# host
  port = 3306, # 默认端口,根据实际修改
  user='testuser',# 用户名
  passwd='1Q2w3e_!', # 密码
  db ='test', # DB name
  charset = 'UTF8',#设置字符编码
  )
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# 使用execute方法执行SQL语句
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取一条数据
data = cursor.fetchone()
print ( 'mysql数据库连接成功:Database version : %s ' % data)
# 关闭数据库连接
db.close()

如下图 image.png 5.查询表数据新建表,手工提前插入部分测试数据

CREATE TABLE `test_tb6` (
   `id` int NOT NULL AUTO_INCREMENT,
   `idcd` varchar(50) DEFAULT NULL,
   `name` varchar(300) DEFAULT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb
# 打开数据库连接
#db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB", charset='utf8' ) #本机localhost连接
#db = MySQLdb.connect("192.168.10.1",3306 "testuser", "test", "1Q2w3e_!", charset='utf8' )
# 创建数据库连接(远程连接)
db = MySQLdb.connect(
  host='192.168.10.1',# host
  port = 3306, # 默认端口,根据实际修改
  user='testuser',# 用户名
  passwd='1Q2w3e_!', # 密码
  db ='test', # DB name
  charset = 'UTF8',#设置字符编码
  )
# 使用cursor()方法获取操作游标
cursor = db.cursor()
querySql = 'select * from test.test_tb6'
print('执行sql'+querySql)
try:
    #执行SQL语句
    cursor.execute(querySql)
    queryResult = cursor.fetchall()
    for row in queryResult:
        idstr = row[0]
        idcdstr = row[1]
        namestr = row[2]
        #打印结果
        print("id=%s,idcd=%s,name=%s"%(idstr, idcdstr,namestr))
except IOError:
   print ("Error: 查询数据错误,unable to fetch data")
# 关闭数据库连接
db.close()

运行程序如下查出表中数据。 image.png 6.向mysql数据库表中插入数据

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb
import uuid
# 创建数据库连接(远程连接)
db = MySQLdb.connect(
  host='192.168.10.1',# host
  port = 3306, # 默认端口,根据实际修改
  user='testuser',# 用户名
  passwd='1Q2w3e_!', # 密码
  db ='test', # DB name
  charset = 'UTF8',#设置字符编码
  )
# 使用cursor()方法获取操作游标
cursor = db.cursor()
#插入操作
random_uuid = uuid.uuid4()
random_num_hex = random_uuid.hex[:32] #生产32位随机数
idcd = random_num_hex
name = '李四'+ random_num_hex
insertSql = "insert into test_tb6(idcd,name) values(%s,%s)"%("'"+idcd+"'","'"+name+"'")
print(insertSql)

try:
    #执行SQL语句
    cursor.execute(insertSql)
    db.commit()
    print('插入数据成功!')
except :
    # 插入错误回滚
    db.rollback()
    print ("Error: 插入数据错误,insert  data error")

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

运行程序如下 image.png 7.更新和删除操作同插入操作,只需要把sql语句修改为updae,delete语句即可,更新删除条件视具体条件而定。

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb
import uuid
# 创建数据库连接(远程连接)
db = MySQLdb.connect(
  host='192.168.10.1',# host
  port = 3306, # 默认端口,根据实际修改
  user='testuser',# 用户名
  passwd='1Q2w3e_!', # 密码
  db ='test', # DB name
  charset = 'UTF8',#设置字符编码
  )
# 使用cursor()方法获取操作游标
cursor = db.cursor()
#修改如下insert语句为update,delete语句
insertSql = "insert into test_tb6(idcd,name) values(%s,%s)"%("'"+idcd+"'","'"+name+"'")
try:
    #执行SQL语句
    cursor.execute(insertSql)
    db.commit()
    print('更新数据成功!')
except :
    # 插入错误回滚
    db.rollback()
    print ("Error: 插入数据错误,insert  data error")

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