解决pymysql一个connection多个游标时,执行不同语句获取数据相同的问题
cursor = conn.cursor()
sql1 = "select * from table_name1;"
cursor.execute(sql1)
relist1 = cursor.fetchall()
sql2 = "select * from table_name2;"
cursor.execute(sql2)
relist2 = cursor.fetchall()
打印relist1和relist2发现结果相同,这是因为一个连接connection同一个时间点只能有一个cursor,执行sql,并获取结果。
解决办法
cursor = conn.cursor()
def select(sql):
cursor.execute(sql)
relist = cursor.fetchall()
return relist
sql1 = "select * from table_name1;"
relist1 = select(sql1)
sql2 = "select * from table_name2;"
relist2 = select(sql2)