Home > database >  Can't update table using "executemany" in python sqlite
Can't update table using "executemany" in python sqlite

Time:03-21

I have to update simple table using below expression:

cur.executemany('UPDATE earths SET population=?, density=?, tilt=?, "land area"=?, dobe=?, livity=? WHERE sid='   str(dc['sid'])   ' AND nr='   str(dc['nr']), v)

Printing the content it gets:

('UPDATE earths SET population=?, density=?, tilt=?, "land area"=?, dobe=?, livity=? WHERE sid=15821 AND nr=8',
 ['1360425627', '2.79', '17.33', '486857065.504', '17.88371', '0.08'])

The error I get is:

ProgrammingError: Incorrect number of bindings supplied. The current statement uses 6, and there are 10 supplied.

I don't know how the program sees 10 values from 6 element list. Any ideas? The table is ok - inserting data one by one gives ok values. It looks

UPDATE earths SET population=1360425627, density=2.79, tilt=17.33, "land area"=486857065.504, dobe=17.88371, livity=0.08 WHERE sid=15821 AND nr=8

CodePudding user response:

Your code would work if you used execute() and maybe this is what you want to do, but for executemany() you should use a tuple of tuples as the 2nd argument, because this is the point of executemany(), to execute the same statement many times and each time supply a different list of parameters:

v = [('1360425627', '2.79', '17.33', '486857065.504', '17.88371', '0.08'),]
cur.executemany('UPDATE ...', v)

CodePudding user response:

executemany expects a nested sequence and interprets v[0] as the first sequence to insert.

It's as if you had used execute(..., v[0]).

It says "10 arguments supplied" because v[0] happens to be a string of length 10.

  • Related