python mysql 语句写在for循环里怎么不起作用了?

def update_valid_email():

conn = MySQLdb.connect('localhost', 'andy', '123123', 'yp', charset='utf8', use_unicode=True)
cursor = conn.cursor()
valid_email = []
with open('C:/Users/Administrator/Desktop/office supply valid.txt','r') as f:
    for i in f:
        valid_email.append(str(i))

for email in valid_email:
    print(email)
    update_sql = """update yp_office_supply set valid = '%s' where email = '%s' """ % ('yes', email)
    cursor.execute(update_sql)
    conn.commit()
    
    
    

单条语句试了下可以成功,可是写在for循环里就不起作用了。请问是不是哪里写错了?
谢谢。

阅读 3.7k
1 个回答

一个游标只能执行一个SQL,把cursor = conn.cursor()放入循环就可以了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题