kjo
kjo

Reputation: 35311

psycopg2 executemany not working

If I run

cur.execute('SELECT * FROM some_table WHERE some_column = %s;', ('some_value',))

then a call to cur.fetchall() immediately afterwards succeeds, and produces an output that is entirely consistent with the result I get if I run the following in psql:

some_db=> SELECT * FROM some_table WHERE some_column = 'some_value';

But if I run

cur.executemany('SELECT * FROM some_table WHERE some_column = %s;',
                [('some_value',)])

...which should be basically equivalent to the cur.execute(...) statement before, the subsequent call to cur.fetchall() raises an exception:

ProgrammingError: no results to fetch

I get the same exception if the sequence passed as second argument in the call to executemany has multiple entries:

cur.executemany('SELECT * FROM some_table WHERE some_column = %s;',
                [('some_value',), ('some_other_value',)])

...or if I use the pyformat style for passing parameters

cur.executemany('SELECT * FROM some_table WHERE some_column = %(v)s;',
                [{'v': 'some_value'}, {'v': 'some_other_value'}])

What's wrong with my code?

BTW, I'm aware of this earlier question, but the answers there do not help.

Upvotes: 1

Views: 2568

Answers (1)

khampson
khampson

Reputation: 15306

executemany specifically discards any result sets that may be returned, so that is why you are getting the exception.

More info: http://initd.org/psycopg/docs/cursor.html#cursor.executemany

Upvotes: 2

Related Questions