原始文本如下:
try:
with connection.cursor() as cursor:
# SQL查询语句
# 根据 last_id 构建SQL查询语句
if last_id == 0:
# 如果没有读取过数据,从第一条开始
# print(last_id)
sql = "SELECT * FROM phrase01 ORDER BY id ASC LIMIT 1"
else:
# 否则,从上次读取的ID的下一行开始
sql = "SELECT * FROM phrase01 WHERE id > %s ORDER BY id ASC LIMIT 1"
cursor.execute(sql, (last_id,))
运行后会报错:
An error occurred: execute() first
参考https://blog.csdn.net/itguangzhi/article/details/82381743这篇文章,在第一个sql语句下面补充了内容,解决该问题,以下是修改后代码:
try:
with connection.cursor() as cursor:
# SQL查询语句
# 根据 last_id 构建SQL查询语句
if last_id == 0:
# 如果没有读取过数据,从第一条开始
# print(last_id)
sql = "SELECT * FROM phrase01 ORDER BY id ASC LIMIT 1"
cursor.execute(sql)#注意这句必须要有,否则会报错:An error occurred: execute() first
else:
# 否则,从上次读取的ID的下一行开始
sql = "SELECT * FROM phrase01 WHERE id > %s ORDER BY id ASC LIMIT 1"
cursor.execute(sql, (last_id,))
问题解决