2

I want to execute postgres query in python.The table name has to be passed as a parameter.Since the table will be created at run time. I have used dict query param style.But i am getting an error.

 import psycopg2

 CONNECTION_STRING = "dbname='autogist' user='postgres' password=''"
 query = "INSERT INTO %(table)s " +\
            "(vin_id, vin_details_id, price, mileage, dealer_id, created_on, modified_on) " +\
            "VALUES (%(vin_id)s, %(vlookup_id)s, %(price)s, %(mileage)s, %(dealer_id)s,now(),now()) " +\
            "RETURNING id"


params = {"table" : "dealer_vehicle_details_2010_01_02",\
                      "vin_id":"3",\
                      "vlookup_id":"403",\
                      "price":"403",\
                      "mileage":"403",\
                      "dealer_id":"276092"
                  }


 conn=psycopg2.connect(CONNECTION_STRING)
 cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
 cursor.execute(query,params)

TRACEBACK:

 ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (262, 0))

---------------------------------------------------------------------------
 ProgrammingError                          Traceback (most recent call last)

 /home/gridlex/workspace/<ipython console> in <module>()

 /usr/local/lib/python2.6/dist-packages/psycopg2/extras.pyc in execute(self, query, vars)
121         self.index = {}
122         self._query_executed = 1
--> 123         return _cursor.execute(self, query, vars)
124 
125     def callproc(self, procname, vars=None):

ProgrammingError: syntax error at or near "E'dealer_vehicle_details_2010_01_02'"
LINE 1: INSERT INTO E'dealer_vehicle_details_2010_01_02' (vin_id, vi...
1
  • cursor.mogrify(query,params) "INSERT INTO E'dealer_vehicle_details_2010_01_02' (vin_id, vin_details_id, price, mileage, dealer_id, created_on, modified_on) VALUES (E'3', E'403', E'403', E'403', E'276092',now(),now()) RETURNING id" Commented Oct 11, 2012 at 11:58

1 Answer 1

3

The statement you send must be syntactically valid when PREPAREd, which a statement with placeholders for table names is not. You can't use placeholders for table names in prepared statements.

Your options are:

  • Substitute the table name in with regular string substitution, "double quoted". Be very careful with your quoting routine; make sure it doubles any quotes within the table name its self, so the table name double"quote becomes "double""quote". Eg. 'SELECT * FROM "%s"' % quote_ident(tablename). You'd have to roll your own quote_ident as AFAIK psycopg2 doesn't expose a function like that.

  • Send the table name as a query parameter to a PL/PgSQL function that uses EXECUTE ... USING to create a dynamic SQL statement using the table name. PL/PgSQL can use the quote_ident function to provide safer quoting than a home-rolled implementation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.