python sqlite3选择更新的行值

时间:2018-05-06 22:53:11

标签: python python-2.7 sqlite

我想打印SQL查询中更新行的行ID

while True:
  Conn = sqlite3.connect('database.db', timeout=60)
  c = Conn.cursor()
  c.execute("UPDATE row SET value='value' where status='status'")
  #i want to do something here to the updated row above
  Conn.commit()
  Conn.close()

1 个答案:

答案 0 :(得分:0)

您需要执行两次查询(可能会降低您的流程速度)。一个用于选择ID,另一个用于更新相应的ID

while True:
    connection = sqlite3.connect('database.db', timeout=60)
    cursor = connection.cursor()

    # Get IDs to update
    rows = cursor.execute("SELECT id FROM row WHERE status='status'")
    ids = [str(x) for x, in rows]  # Transform tupple list to list of string

    # Update corresponding IDs
    cursor.execute(
        "UPDATE row SET value='value' WHERE id in ({0})".format(','.join(ids)))

    # Do what you want with `ids`...

    connection.commit()
    connection.close()