我正在编写一些 python 代码来对 redshift (postgres) SQL 数据库运行查询,但我遇到了一个问题,我无法从传递给的变量中去除周围的单引号查询。我正在尝试从列表中删除一些表。这是我的代码的基础知识:
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 注入。
慕田峪9158850
元芳怎么了
相关分类