今天就跟大家聊聊有关怎么在python3中对接mysql数据库,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
安装PyMySQL,用pip安装:pip install PyMySQL
查询找到PyMySQL,说明安装好了。

操作数据一般步骤是:
连接数据库 pymysql.connect(host = 'localhost',user = 'root',passwd ='xxxxxxxxx',db = 'fbl' )
创建游标 cursor = db.cursor()
执行sql命令 # cursor.execute(Mysql cmd)
提交 db.commit()
关闭数据库db.close()
#!/usr/bin/env python3
#coding:utf-8
import pymysql
# 打开数据库连接
db = pymysql.connect(host = 'localhost',user = 'root',passwd ='xxxxxxxxx',db = 'fbl' )
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL 查询
# cursor.execute("SELECT VERSION()")
cmd_list = []
cmd_list.append('show tables;')
cmd_list.append('insert into hunpo(title,url) values("adfsf","qml");')
cmd_list.append('insert into hunpo(title,url) values("qml","fbl");')
cmd_list.append('select * from hunpo;')
for cmd in cmd_list:
cursor.execute(cmd)
data = cursor.fetchone()
print(data)
#提交数据
db.commit()
# 关闭数据库连接
db.close()

看完上述内容,你们对怎么在python3中对接mysql数据库有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注天达云行业资讯频道,感谢大家的支持。