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

  • Home
  • SEARCH
  • 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 7521321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:12:58+00:00 2026-05-30T02:12:58+00:00

When specifying a parameter to execute() in psycopg2 in Python, like this: cursor.execute(‘SELECT *

  • 0

When specifying a parameter to execute() in psycopg2 in Python, like this:

cursor.execute('SELECT * FROM %s', ("my_table", ))

I’m getting this error:

psycopg2.ProgrammingError: syntax error at or near "'my_table'"
LINE 1: SELECT * FROM 'my_table'

What am I doing wrong? It looks like psycopg2 is adding single quotes to the query, and those single quotes are causing the syntax error.

If I don’t use a parameter, it works correctly:

cursor.execute('SELECT * FROM my_table')
  • 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-05-30T02:12:59+00:00Added an answer on May 30, 2026 at 2:12 am

    I believe that parametrized statements like this are meant to be used with values and not table names (or SQL keywords, etc.). So you’re basically out of luck with this.

    However, do not worry, as this mechanism is meant to prevent SQL injection, and you normally know what table you want to access at code-writing time, so there is little chance somebody may inject malicious code. Just go ahead and write the table in the string.

    If, for some (possibly perverse) reason you keep the table name parametric like that:

    1. If the table name comes from your program (e.g. a dictionary, or class attribute), then do the usual string substitution.
    2. If the table name comes from the external world (think “user input”): either don’t do that, or trust the user completely and apply the previous approach 1.

    For example:

    cursor.execute(
        'SELECT * FROM %s where %s = %s'
        % ("my_table", "colum_name", "%s"), #1
        ("'some;perverse'string;--drop table foobar")) #2
    

    #1: Let the third %s be replaced with another ‘%s’ at this time, to allow later processing by psycopg2
    #2: This is the string that will be properly quoted by psycopg2 and placed instead of that third ‘%s’ in the original string

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

Sidebar

Related Questions

I am getting an error when trying to use a Type parameter when specifying
I wrote a method which exports values to excel file from an IEnumerable parameter.
I tried changing a default parameter value with this: ALTER PROCEDURE [dbo].[my_sp] @currentDate datetime
i am uploading photo to application(or like) profile page. But it will show error
i am trying to execute all tests in subfolders of my test directory. This
So I setup C# parameter validation using the method described in this blog post.
By specifying -lib foo.jar to ant I get the behaviour that the classes in
I'm specifying my doctype as xhtml strict, but it's being sent over the wire
I'm specifying a color hex code as the background-color of a really long div.
I have requirement of specifying web part connections in onet.xml. So when site is

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.