mogrify and returning with psycopg2 mogrify and returning with psycopg2 postgresql postgresql

mogrify and returning with psycopg2


The problem was that I thought I had to put an string parameter interpolation for every value to insert. I'll explain it:

Let's say I have a list with 2 dictionaries (with length of three) with the data to insert:

lst = [{'a': 21, 'b': 9, 'c': 33}, {'a': 76, 'b': 84, 'c': 46}]

In order to insert those values I did something similar to:

query = curs.mogrify("INSERT INTO {} ({}) VALUES {} RETURNING {}".format(                        table,                        ', '.join(lst[0].keys()),                        ', '.join(['%s'] * len(lst[0].values())),                        'id'                    ), [tuple(v.values()) for v in lst])

appending a total of three '%s' (one for every item in a single dict in lst). The result for this is the exception message error list index out of range and this happens because curs.mogrify() is expecting just one '%s' for every dict in lst, so for this case it only needs two '%s' instead three.

Therefore, the correct form to insert data from lst is:

query = curs.mogrify("INSERT INTO {} ({}) VALUES {} RETURNING {}".format(                            table,                            ', '.join(lst[0].keys()),                            ', '.join(['%s'] * len(lst)),                            'id'                        ), [tuple(v.values()) for v in lst])

Replacing len(lst[0].values()) by len(lst).

This how I solved my problem (I did not understand this because I did not read the code of the example properly). I hope this helps.

Sam