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()