鍍金池/ 問答/Python  數(shù)據(jù)庫  HTML/ python mysql 語句寫在for循環(huán)里怎么不起作用了?

python mysql 語句寫在for循環(huán)里怎么不起作用了?

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循環(huán)里就不起作用了。請問是不是哪里寫錯了?
謝謝。

回答
編輯回答
她愚我

一個游標(biāo)只能執(zhí)行一個SQL,把cursor = conn.cursor()放入循環(huán)就可以了

2017年5月4日 06:16