#!/usr/bin/python3
import pymysql
# 打开数据库连接
db = pymysql.connect("118.25.52.34", "用户名", "数据库密码", "test")
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# SQL 插入语句
sql = "INSERT INTO student(name, \
tel, age) \
VALUES ('%s', '%d', '%d' )" % \
('M31', 20, 2000)
try:
# 执行sql语句
cursor.execute(sql)
# 执行sql语句
db.commit()
except:
# 发生错误时回滚
db.rollback()
# 关闭数据库连接
db.close()
python链接Mysql数据库
最新推荐文章于 2021-01-30 05:29:32 发布