python sqlite3 for loop update

python

Solution

Use `data = data.fetchall()` before your loop. Otherwise you wind up recycling the cursor inside of your loop (resetting its result set) while you're trying to loop over that result set.

Using `.fetchall()` returns a list of results so that you have them stored locally before you re-use the cursor.

Alternatively, create a separate cursor to use for your update statements if you don't want to cache the results of the first query locally.

Problem

``` import sqlite3 conn = sqlite3.connect('sample.db') cursor = conn.cursor() data = cursor.execute('''SELECT * From Table''') for i in data: title = i[0] status = i[1] cursor.execute('''UPDATED Table SET status=? WHERE title=?''', (status, title)) cursor.close() conn.commit() ``` I am trying to update over multiple iterations. However, the script breaks out of the loop as soon as the database makes the first update. How to fix this? Thanks!

Original source