I am trying to UPDATE all records of a table with the results of a function which uses the other rows of the table as arguments. However, the result of this operation for all records is repeated from the first record. Can anyone explain why this might be the case?
def fun(a,b,c,d):
return a + b + c + d
cur = conn.cursor()
cur.execute("SELECT field1, field2, field3, field4 FROM TABLE1")
for row in cur:
cur.execute("UPDATE TABLE1 SET field5 = ?", (fun(row[0],row[1],row[2],row[3]),))
The completed table looks like this:
field1, field2, field3, field4, field5
4, 3, 2, 1, 10
7, 3, 1, 0, 10
8, 5, 2, 0, 10
When it should look like this:
field1, field2, field3, field4, field5
4, 3, 2, 1, 10
7, 3, 1, 0, 11
8, 5, 2, 0, 15