Psycopg2 Executemany With Simple List?
I'm trying to use psycopg2 executemany for a simple multi-insert but I can only make it work using dict and not 'plain' sequence of values: # given: values = [1, 2, 3] ; cursor = c
Solution 1:
executemany
expects a sequence of sequences, eg. a list of lists:
[[v] for v in values]
Solution 2:
executemany()
takes a list of parameters and each single parameter should be an object that works with execute()
, i.e., a tuple
or a dict
, but not a simple value like a number or string. That's why the second version is OK: you're generating multiple dict
s. You can also just write:
values = [(1,), (2,), (3,)]
where each element of the list is a tuple
.
Post a Comment for "Psycopg2 Executemany With Simple List?"