psycopg2 查询没有正确解析参数?
psycopg2 query not parsing parameters properly?
将 python 2.7.12 与 psycopg2 版本 2.6.2 一起使用,但没有成功提交生成的查询(与我刚输入的字符串相反)。查询 AWS RedShift 实例。
当我尝试 运行 代码时,发生的事情是它失败了,因为添加了一个无关的括号(当我使用 (%s)
构造时......或者添加了一个额外的单引号,如果我只使用 %s
代替)。我已经尝试非常仔细地遵循文档,并且还在此处和 google 进行了搜索,但一无所获。有没有人对如何解决这个问题有任何建议?
我已经非常努力地遵循 http://initd.org/psycopg/docs/usage.html#query-parameters 上的文档:
我的代码如下所示:
con = psycopg2.connect(dbname=dbname, host=host, port=port, user=user, password=password)
cur = con.cursor()
try3 = "TRUNCATE TABLE (%s);"
values = ("schema_one.tbl_six",)
cur.execute(try3,values)
try4 = "TRUNCATE TABLE %s;"
values = ("schema_four.tbl_four",)
cur.execute(try4,values)
产生此输出:
$ ./test_qry.py
Traceback (most recent call last):
File "./test_qry.py", line 23, in <module>
cur.execute(try3,values)
psycopg2.ProgrammingError: syntax error at or near "("
LINE 1: TRUNCATE TABLE ('schema_one.tbl_six');
$ ./test_qry.py
Traceback (most recent call last):
File "./test_qry.py", line 28, in <module>
cur.execute(try4,values)
psycopg2.ProgrammingError: syntax error at or near "'schema_four.tbl_four'"
LINE 1: TRUNCATE TABLE 'schema_four.tbl_four';
您有一个例外情况,稍后将在 docs 中描述:
Only variable values should be bound via this method: it shouldn’t be
used to set table or field names. For these elements, ordinary string
formatting should be used before running execute().
换句话说,你不能参数化table或列名,必须使用字符串格式:
query = "TRUNCATE TABLE %s;"
values = ("schema_one.tbl_six",)
cur.execute(query % values)
或者,str.format()
:
query = "TRUNCATE TABLE {table_name};"
cur.execute(query.format(table_name="schema_one.tbl_six"))
也就是说,即使您信任来源,您仍然应该小心 validate/escape table 名称以防止 SQL injection attacks。
将 python 2.7.12 与 psycopg2 版本 2.6.2 一起使用,但没有成功提交生成的查询(与我刚输入的字符串相反)。查询 AWS RedShift 实例。
当我尝试 运行 代码时,发生的事情是它失败了,因为添加了一个无关的括号(当我使用 (%s)
构造时......或者添加了一个额外的单引号,如果我只使用 %s
代替)。我已经尝试非常仔细地遵循文档,并且还在此处和 google 进行了搜索,但一无所获。有没有人对如何解决这个问题有任何建议?
我已经非常努力地遵循 http://initd.org/psycopg/docs/usage.html#query-parameters 上的文档:
我的代码如下所示:
con = psycopg2.connect(dbname=dbname, host=host, port=port, user=user, password=password)
cur = con.cursor()
try3 = "TRUNCATE TABLE (%s);"
values = ("schema_one.tbl_six",)
cur.execute(try3,values)
try4 = "TRUNCATE TABLE %s;"
values = ("schema_four.tbl_four",)
cur.execute(try4,values)
产生此输出:
$ ./test_qry.py
Traceback (most recent call last):
File "./test_qry.py", line 23, in <module>
cur.execute(try3,values)
psycopg2.ProgrammingError: syntax error at or near "("
LINE 1: TRUNCATE TABLE ('schema_one.tbl_six');
$ ./test_qry.py
Traceback (most recent call last):
File "./test_qry.py", line 28, in <module>
cur.execute(try4,values)
psycopg2.ProgrammingError: syntax error at or near "'schema_four.tbl_four'"
LINE 1: TRUNCATE TABLE 'schema_four.tbl_four';
您有一个例外情况,稍后将在 docs 中描述:
Only variable values should be bound via this method: it shouldn’t be used to set table or field names. For these elements, ordinary string formatting should be used before running execute().
换句话说,你不能参数化table或列名,必须使用字符串格式:
query = "TRUNCATE TABLE %s;"
values = ("schema_one.tbl_six",)
cur.execute(query % values)
或者,str.format()
:
query = "TRUNCATE TABLE {table_name};"
cur.execute(query.format(table_name="schema_one.tbl_six"))
也就是说,即使您信任来源,您仍然应该小心 validate/escape table 名称以防止 SQL injection attacks。