ahq
ahq

Reputation: 71

parameterised postgresql select statement using python

sql="select %s,tablename from pg_table_def where tablename like (%s)"

data=("schemaname","abc",)

cur.execute(sql,data)

If I pass a value as described above, then the select takes it as a string. Which is not the intention.

If I try

data=(schemaname,"abc",)

then it shows the error global name 'schemaname' is not defined.

Upvotes: 0

Views: 46

Answers (1)

Mureinik
Mureinik

Reputation: 311796

You cannot parameterize object name (in this case, a column name) that way. You could instead resort to string manipulation:

column = "schemaname"
sql = "select {}, tablename from pg_table_def where tablename like (%s)".format(column) 
data= ("abc",)

cur.execute(sql,data)

Upvotes: 2

Related Questions