Python 对于在使用pymssql进行n次迭代后进行输入确认的循环
我正在使用以下代码的字典更新数据库表:Python 对于在使用pymssql进行n次迭代后进行输入确认的循环,python,pymssql,Python,Pymssql,我正在使用以下代码的字典更新数据库表: cursor = conn.cursor() for key in dRt: x = dRt[key] sql = 'UPDATE table SET R = %s WHERE %s = ID' cursor.execute(sql, (x, key)) conn.commit() conn.close() 我的字典有几千条词条
cursor = conn.cursor()
for key in dRt:
x = dRt[key]
sql = 'UPDATE table SET R = %s WHERE %s = ID'
cursor.execute(sql, (x, key))
conn.commit()
conn.close()
我的字典有几千条词条。是否可以向代码中添加一个部分,该部分在将1000行写入数据库后询问是否应该继续
我试过这样的方法:
cursor = conn.cursor()
counter = 0
for key in dRt:
x = dRt[key]
sql = 'UPDATE table SET R = %s WHERE %s = ID'
if counter == 1000:
break
eingabe = input("Beenden? Enter drücken!\n")
cursor.execute(sql, (x, key))
conn.commit()
conn.close()
但这不能正常工作。基于计数器有条件地初始化输入应该可以工作。初始化输入后,还应将计数器重置为0
cursor = conn.cursor()
counter = 0
for key in dRt:
counter += 1
x = dRt[key]
sql = 'UPDATE table SET R = %s WHERE %s = ID'
if counter == 1000:
input("Beenden? Enter drücken!\n")
counter = 0
cursor.execute(sql, (x, key))
conn.commit()
conn.close()
在循环中有一个
input
语句是可以的,但是执行将停止,直到输入了某个内容。然后,您必须检查输入的内容,以确定是否要继续:
cursor = conn.cursor()
sql = 'UPDATE table SET R = %s WHERE %s = ID' # moved out of the loop
counter = 0
for key in dRt:
counter += 1
x = dRt[key]
cursor.execute(sql, (x, key)) # moved higher in the loop
if counter == 1000: # now we have done the next 1000 updates
reply = input("Beenden? Enter drücken!\n")
if reply == '':
break # anything else entered than just enter, for example "geh", and we continue
counter = 0
conn.commit()
conn.close()
您没有增加
计数器
这只会更新一次,因为execute
不在循环中,而且调用input
的结果从未经过测试,因此没有任何用途。