遍历各个行并更新这些行SQLite3 Python [英] Loop through individual rows and update those rows SQLite3 Python

查看:81
本文介绍了遍历各个行并更新这些行SQLite3 Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表: http://prntscr.com/gc9oat

当我在

if coinMarketValue > value and notified == 0:

这是我目前拥有的代码

connection = sqlite3.connect('notify.db')

c = connection.cursor()    
c.execute('SELECT * FROM notifyTable')
data = c.fetchall()


for row in data:
    authorID = row[1]
    coin = row[2]
    operator = row[3]
    value = row[4]
    coinMarketValue = row[5]
    notified = row[6]

    if operator == '<':           
        if coinMarketValue < value and notified == 0: #notified hasnt been set true 
            print("coin market value is less than value so updating notifed to True")             
            connection.execute("UPDATE notifyTable set notified = 'True' WHERE coinMarketValue < value AND notified == 0")
            connection.commit()

好的,现在代码遍历每一行,并且条件为True.

Right, now the code goes through each row and if the condition is True.

coinMarketValue < value AND notified == 0

然后它将所有Notified列更新为True-而不是仅更新当前行.

Then it will update the all the Notified columns to True - instead of just updating the current row.

所以,我如何只更新脚本当前正在处理的行上的Notified(而不是更新所有行)

So, how can I only update Notified on the row the script is currently working on (instead of updating all the rows)

谢谢

推荐答案

如果 taskname 是主键,则可以执行以下操作:

If taskname is the primary key you can do this:

for row in data:
    taskname = row[0]
    # ...

    if operator == '<':           
        if coinMarketValue < value and notified == 0: #notified hasnt been set true 
            # ...
            connection.execute("""UPDATE notifyTable 
                                  SET notified = 'True' 
                                  WHERE coinMarketValue < value 
                                  AND notified = 0
                                  AND taskName = ?""",(taskname,))

这篇关于遍历各个行并更新这些行SQLite3 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆