我正在研究 python。我想用新值更新每一行 sql。 我的代码是:
val = cursor.execute("select id from tweeter1")
words= processedRow.split()
fdist2=len(words)
for id1 in val:
cursor.execute("""UPDATE TWEETER1 SET t1=%s where id = %s""",(fdist2,id1))
db.commit()
当我执行这段代码时,我收到一条错误消息:
for id1 in val: TypeError: 'long' object is not iterable
任何帮助将不胜感激。谢谢
最佳答案
您必须遍历光标对象:
cursor.execute("select id from tweeter1")
words = processedRow.split()
fdist2 = len(words)
for id1 in cursor.fetchall():
cursor.execute("""UPDATE TWEETER1 SET t1=%s where id = %s""",(fdist2,id1))
db.commit()
但是只要您将所有行更改为相同的值,您只需要更新一次而无需任何 where 子句:
words = processedRow.split()
fdist2 = len(words)
cursor.execute("""UPDATE TWEETER1 SET t1=%s""",(fdist2,))
db.commit()
关于python - 使用新值更新 sql 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37510432/