SQL = "INSERT INTO authors (name) VALUES (%s);" # Note: no quotes
data = ("O'Reilly", )
cur.execute(SQL, data)
SQL = "INSERT INTO authors (%s) VALUES (%s);"
data_1 = ("name",)
data_2 = ("O'Reilly", )
cur.execute(SQL, data_1,data_2)
Only query values should be bound via this method: it shouldn’t be used to merge table or field names to the query (Psycopg will try quoting the table name as a string value, generating invalid SQL). If you need to generate dynamically SQL queries (for instance choosing dynamically a table name) you can use the facilities provided by the psycopg2.sql module:
>>> cur.execute("INSERT INTO %s VALUES (%s)", ('numbers', 10)) # WRONG >>> cur.execute( # correct ... SQL("INSERT INTO {} VALUES (%s)").format(Identifier('numbers')), ... (10,))