dvn
dvn

Reputation: 41

sqlite3 .executemany: Incorrect number of bindings supplied

I'm running this line:

    c.executemany('INSERT INTO frame VALUES (?,?)', list( ((f[0],), tuple(f[1])) )   )

This is f: ('Constructed_Restraints', ['hogtie', 'hobble'])

So this is what the 2nd argument of my .executemany line becomes:

[('Constructed_Restraints',), ('hogtie', 'hobble')]

However, I'm getting this error:

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 2, and there are 1 supplied.

I've tried nearly everything in various SO answers to other questions, but I continue getting this error. How am I only providing 1 binding? The list has 2 tuples in it.

Upvotes: 0

Views: 2815

Answers (1)

Chris Travers
Chris Travers

Reputation: 26464

Assuming you want two rows inserted with a null in the second field, try this:

c.executemany('INSERT INTO frame VALUES (?,?)', list( ((f[0],None), tuple(f[1])) )

On the other hand, if you want one row inserted with some sort of serialised value, try:

c.execute('INSERT INTO frame VALUES (?,?)', (f[0], ','.join(f[1])) )

The latter will insert the second value in comma separated value syntax which poses normalisation problems. Going beyond this probably requires database thinking, but SQLite is pretty capable so the query issues can probably be handled in place (insert and update anomalies on the other hand are different).

Upvotes: 1

Related Questions