Skip to content Skip to sidebar Skip to footer

Use Update In A Loop

When I execute the following code: for acc_row in cursor.execute('select * from tabela1'): cursor.execute('UPDATE table_name SET column_name=? where column_name=?', ('some_valu

Solution 1:

You are relying on the state of the cursor. object to control the for loop, but then you are modifying that object (by executing the UPDATE statement) inside the loop. You'll need to maintain two cursor objects, one to control the loop and another one to perform the UPDATEs.

Solution 2:

If your result set isn't large this should work:

rows = cursor.execute('select * from tabela1').fetchall()
for acc_row in rows:
    cursor.execute('UPDATE table_name SET column_name=? where column_name=?', (acc_row.column1, acc_row.column2))

If the result set is too large to load into memory, maintain two cursors as mentioned in Gord Thompson's answer.

Post a Comment for "Use Update In A Loop"