python中execute是啥_python – 为什么“c.execute(…)”打破循环?

我正在尝试更改sqlite3文件中的一些数据,而我在

python和google-fu中不存在的知识让我最终得到了这段代码:

#!/usr/bin/python

# Filename : hello.py

from sqlite3 import *

conn = connect('database')

c = conn.cursor()

c.execute('select * from table limit 2')

for row in c:

newname = row[1]

newname = newname[:-3]+"hello"

newdata = "UPDATE table SET name = '" + newname + "', originalPath = '' WHERE id = '" + str(row[0]) + "'"

print row

c.execute(newdata)

conn.commit()

c.close()

它就像第一行的魅力一样,但由于某种原因它只运行一次循环(只有表中的第一行被修改).当我删除“c.execute(newdata)”时,它会循环遍历表中的前两行,就像它应该的那样.我如何使其工作?

最佳答案 这样做是因为只要你执行c.execute(newdata),光标就不再指向原始结果集了.我会这样做:

#!/usr/bin/python

# Filename : hello.py

from sqlite3 import *

conn = connect('database')

c = conn.cursor()

c.execute('select * from table limit 2')

result = c.fetchall()

for row in result:

newname = row[1]

newname = newname[:-3]+"hello"

newdata = "UPDATE table SET name = '" + newname + "', originalPath = '' WHERE id = '" + str(row[0]) + "'"

print row

c.execute(newdata)

conn.commit()

c.close()

conn.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值