Simple SQL statement:
sql = "DROP TABLE IF EXISTS %s"
cursor.execute(sql, ("user_table",))
It fails with:
psycopg2.ProgrammingError: syntax error at or near "'user_table'"
LINE 1: DROP TABLE IF EXISTS 'user_table'
The single quotes in the SQL statement are the problem. If I run this it works just fine:
cursor.execute("DROP TABLE IF EXISTS user_table")
您只能将参数语法用于参数,而不能用于表或列名称。
The AsIs
adapter is useful for objects whose string representation is already valid as SQL representation
from psycopg2.extensions import AsIs
sql = "DROP TABLE IF EXISTS %s"
cursor.execute(sql, (AsIs("user_table"),))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.