【发布时间】:2019-02-22 20:06:43
【问题描述】:
我正在编写一些 python 代码来运行对 redshift (postgres) SQL 数据库的查询,我遇到了一个问题,我无法从变量 I' 中删除周围的单引号m 传递给查询。我正在尝试从列表中删除一些表。这是我的代码的基础:
def func(table_list):
drop_query = 'drop table if exists %s' #loaded from file
table_name = table_list[0] #table_name = 'my_db.my_table'
con=psycopg2.connect(dbname=DB, host=HOST, port=PORT, user=USER, password=PASS)
cur=con.cursor()
cur.execute(drop_query, (table_name, )) #this line is giving me trouble
#cleanup statements for the connection
table_list = ['my_db.my_table']
调用 func() 时,出现以下错误:
syntax error at or near "'my_db.my_table'"
LINE 1: drop table if exists 'my_db.my_table...
^
有没有办法从列表项中删除周围的单引号?
暂时,我做错了(认为是什么)并使用了字符串连接,但知道这基本上是在乞求 SQL 注入。
【问题讨论】:
标签: python sql list amazon-redshift