1

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.

Community
  • 1
  • 1
kjo
  • 33,683
  • 52
  • 148
  • 265

1 Answers1

2

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

khampson
  • 14,700
  • 4
  • 41
  • 43