首页 > 数据库技术 > 详细

pymysql编写脚本

时间:2021-06-04 12:19:15      阅读:16      评论:0      收藏:0      [点我收藏+]

import pymysql

def insertsql():
# 建立数据库连接
connection_sql = pymysql.connect(
host=‘127.0.0.1‘,
port=3306,
user=‘root‘,
password=‘root‘,
db=‘class202102‘,
charset=‘utf8‘
)
# 获取游标
cursor = connection_sql.cursor(pymysql.cursors.DictCursor)
str1 = ‘1‘
str2 = ‘18‘
sql = ‘select * from students where id> %s and age > %s‘
print(sql)

try:
cursor.execute(sql,[str1,str2])
a = cursor.fetchall()
print(a)
connection_sql.commit()
except Exception as e:
connection_sql.rollback()
raise e

# 关闭游标
cursor.close()

pymysql编写脚本

原文:https://www.cnblogs.com/beijin/p/14848123.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!