Re: Sanitize schema name

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Dorian Hoxha <dorian(dot)hoxha(at)gmail(dot)com>
Cc: Ludovic Gasc <gmludo(at)gmail(dot)com>, psycopg(at)postgresql(dot)org
Subject: Re: Sanitize schema name
Date: 2015-05-07 13:33:29
Message-ID: 554B69A9.2070209@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On 05/07/2015 04:35 AM, Dorian Hoxha wrote:
> He wants to dynamically pass the table name:
>
> cursor.execute("SELECT * FROM %s", (table,)) # won't work
>

How about using format()?:
http://www.postgresql.org/docs/9.4/static/functions-string.html#FUNCTIONS-STRING-FORMAT

Available 9.1+

cur.execute("select format('select * from %I', 'student_info')")

sql_str = cur.fetchone()[0]

sql_str
'select * from student_info'

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Ludovic Gasc 2015-05-07 20:06:52 Re: Sanitize schema name
Previous Message Karsten Hilbert 2015-05-07 12:28:37 Re: Sanitize schema name