psycopg2 没有从元组参数中转义引号?使用 sql.SQL 格式化参数
psycopg2 not escaping quotation from tuple arguments? Using sql.SQL to format arguments
我正在尝试使用 psycopg2 在 postgres 中动态创建 table。似乎在传递参数时,pyscopg2 没有转义引号并理解我的查询。代码如下所示:
input_string_tup = ('col1 int', 'col2 varchar(2)', ...)
create_table_str = sql.SQL("CREATE TABLE {} ({data})").format(
sql.SQL(table_name),
data=sql.SQL(", ").join(sql.Composed(sql.Identifier(i) for i in input_string_tup)
))
execute_batch(cur, create_table_str, [input_string_tup])
我遇到的错误:
psycopg2.errors.SyntaxError: syntax error at end of input LINE 1: ...",
"shape_area numeric(8, 6)", "shape_length numeric(8, 6)")
^
什么打印(create_table_str.as_string(conn))输出:
CREATE TABLE my_table ("col1 int", "col2 varchar(2)", "col3 int", ... )
编辑以显示没有字符串连接的修改后的答案
input_string_tup = (('col1', 'int'), ('col2, varchar(2)'), ...)
create_table_str = sql.SQL("CREATE TABLE {} ({data})").format(
sql.SQL(table_name),
data=sql.SQL(", ").join(sql.Composed([sql.Identifier(i[0]),
sql.SQL(' '), sql.SQL(i[1])]) for i in input_string_tup))
感谢阿德里安的帮助
sql.Identifier 仅适用于 SQL 个对象,例如table/column 命名而不是类型:
https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
尝试:
input_string_tup = (('col1', 'int'), ('col2', 'varchar(2)'))
table_name = 'test_table'
create_table_str = sql.SQL("CREATE TABLE {} ({data})").format(
sql.SQL(table_name),
data=sql.SQL(", ").join(sql.Composed(sql.Identifier(i[0]) + sql.SQL(' ' + i[1]) for i in input_string_tup)))
print(create_table_str.as_string(con))
CREATE TABLE test_table ("col1" int, "col2" varchar(2))
cur.execute(create_table_str)
我正在尝试使用 psycopg2 在 postgres 中动态创建 table。似乎在传递参数时,pyscopg2 没有转义引号并理解我的查询。代码如下所示:
input_string_tup = ('col1 int', 'col2 varchar(2)', ...)
create_table_str = sql.SQL("CREATE TABLE {} ({data})").format(
sql.SQL(table_name),
data=sql.SQL(", ").join(sql.Composed(sql.Identifier(i) for i in input_string_tup)
))
execute_batch(cur, create_table_str, [input_string_tup])
我遇到的错误:
psycopg2.errors.SyntaxError: syntax error at end of input LINE 1: ...",
"shape_area numeric(8, 6)", "shape_length numeric(8, 6)")
^
什么打印(create_table_str.as_string(conn))输出:
CREATE TABLE my_table ("col1 int", "col2 varchar(2)", "col3 int", ... )
编辑以显示没有字符串连接的修改后的答案
input_string_tup = (('col1', 'int'), ('col2, varchar(2)'), ...)
create_table_str = sql.SQL("CREATE TABLE {} ({data})").format(
sql.SQL(table_name),
data=sql.SQL(", ").join(sql.Composed([sql.Identifier(i[0]),
sql.SQL(' '), sql.SQL(i[1])]) for i in input_string_tup))
感谢阿德里安的帮助
sql.Identifier 仅适用于 SQL 个对象,例如table/column 命名而不是类型: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
尝试:
input_string_tup = (('col1', 'int'), ('col2', 'varchar(2)'))
table_name = 'test_table'
create_table_str = sql.SQL("CREATE TABLE {} ({data})").format(
sql.SQL(table_name),
data=sql.SQL(", ").join(sql.Composed(sql.Identifier(i[0]) + sql.SQL(' ' + i[1]) for i in input_string_tup)))
print(create_table_str.as_string(con))
CREATE TABLE test_table ("col1" int, "col2" varchar(2))
cur.execute(create_table_str)