I use psycopg2
in python (2.7.10) to connect to a postgresql DB. The docs are pretty clear about composition of dynamic SQL statements:
Never, never, NEVER use Python string concatenation (+) or string parameters interpolation (%) to pass variables to a SQL query string. Not even at gunpoint.
In psycopg2
version 2.7 there's the new sql
module to do this string composition in a way that's safe against SQL injection. I nevertheless don't understand how to properly construct a statement like:
import psycopg2 as ps
C = psycopg.connect(host='my_host', port=Port, database='My_DB')
cur = C.cursor()
schema = 'some_schema'
table = 'some_table'
SQL = cur.execute("SELECT * FROM "+schema+"."+table+";") # This is horribly wrong
SQL = cur.execute("SELECT * FROM some_schema.some_table;") # That's what the result should be