How to use pgdb.executemany?

I am trying to execute insert from Python on PostgreSQL using the pgdb module.

I see that the documentation says:

  cursor.executemany(query, list of params) # Execute a query many times, binding each param dictionary # from the list. 

So I'm trying things like:

 >>> insert = "insert into foo (name, number) values (?,?);" >>> params = [{ 'name': 'John', 'number': 123 }, { 'name': 'Jack', 'number': 234 }] >>> cursor.executemany(insert, params) 

Does this give me an error pointing to ? . What is the correct syntax for such a parameterized query? Also, if indicated in the documentation, where can I find it?

+6
source share
1 answer

Do you want to

 insert = "insert into foo (name, number) value (%(name)s, %(number)s);" 

What style for enabling parameters is supported by your DBAPI driver can be found with the paramstyle module level constant. According to PEP 249 (the so-called python database API 2 specification):

A string constant indicating the type of marker for the formatting parameter expected by the interface. Possible values: [2]:

 'qmark' Question mark style, eg '...WHERE name=?' 'numeric' Numeric, positional style, eg '...WHERE name=:1' 'named' Named style, eg '...WHERE name=:name' 'format' ANSI C printf format codes, eg '...WHERE name=%s' 'pyformat' Python extended format codes, eg '...WHERE name=%(name)s' 

and if you check pgdb, you will see that

 >>> import pgdb >>> pgdb.paramstyle 'pyformat' 
+6
source

Source: https://habr.com/ru/post/893035/


All Articles