Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8656995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:24:50+00:00 2026-06-12T15:24:50+00:00

I want to execute postgres query in python.The table name has to be passed

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:24:51+00:00Added an answer on June 12, 2026 at 3:24 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to execute a parameterized query to perform a search by user-supplied parameters.
I want to execute an SQL query and get its result in elisp: (let
I want to run a SELECT statement and I want execute a DELETE statement
I want to execute a jquery script that will fade a <label for='username'> after
I want to execute a single Query (or Stored Proc with multiple resultsets). I
I want to execute a mapreduce query, in erlang, that contains two map phases
I'm trying to execute a query like this: SELECT * FROM table WHERE id
i want to install postgres on centos 5.3 via rpm, i execute command rpm
I want to execute the Python scripts(that displays a toast and notification) in Android
I want to execute an SQL query like: select 'tb1'.'f1','tb1'.'f2','tb2'.'f1' from 'tb1','tb2'; Now the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.