1

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
forpas
  • 160,666
  • 10
  • 38
  • 76
Peter.k
  • 1,475
  • 23
  • 40

2 Answers2

2

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)
forpas
  • 160,666
  • 10
  • 38
  • 76
  • Strangely, tuple `v Out[853]: ('127830056', '0.58', '22.06', '218662597.847', '37.27435', '0.41')` of len 6 make error: `ProgrammingError: Incorrect number of bindings supplied. The current statement uses 6, and there are 9 supplied.` I hide 3 fields somewhere! – Peter.k Mar 20 '22 at 20:21
  • @Peter.k use execute() and not eecutemany() if v=('127830056', '0.58', '22.06', '218662597.847', '37.27435', '0.41') – forpas Mar 20 '22 at 20:23
  • Ok. Found it: As You said: ` [('127830056', '0.58', '22.06', '218662597.847', '37.27435', '0.41')]` works. I used `[]` on `tuple`, Now it has len=1, but inserts those values. – Peter.k Mar 20 '22 at 20:27
  • 1
    @Peter.k but if what you want is to execute the statement only once then you should use execute() and v = ('127830056', '0.58', '22.06', '218662597.847', '37.27435', '0.41'). – forpas Mar 20 '22 at 20:29
  • Now i get it and everything is clear. Thanks! – Peter.k Mar 20 '22 at 20:32
1

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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65