python 操作 mysql数据库很简单
与其他语言的jdbc操作流程一致。
1、创建数据库connection连接
2、创建游标
3、执行sql语句
4、提交事务
5、关闭connection连接。
import pymysql
#创建数据库连接对象
db=pymysql.connect(host='192.168.240.165',user='root',passwd='123456',db='test',charset='utf8')
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL 语句,也可以执行 insert,delete等操作
cursor.execute("SELECT * from test")
#事务提交
#db.commit()
# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchone()
print ( data)
# 关闭数据库连接
db.close()