python - 使用新值更新 sql 中的行

标签 python mysql

我正在研究 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/

相关文章:

python - 如何使用日期时间索引在 Pandas 中进行插值重建索引?

Python 3,为什么只有函数和部分在多处理 apply_async 中起作用,但闭包和 lambda 都不起作用

php - 连接到数据库 - 本地 PostgreSQL - PHP 应用程序

mysql - 每隔几分钟运行一次 cronjob 来启动 mysqld 有哪些潜在的负面影响?

python - 我应该如何在opencv中裁剪出非矩形,非多边形的ROI?

Python 子流程(输出 PIPE)

python - 运行 Python 3.5 解释器需要哪些标准库模块?

php - SELECT 查询计数未输出

mysql - 如何在学说中插入非常引用

MySQL : add constraint gives:ERROR 1452 (23000): Cannot add or update a child row?