我写了一个Python函数来获取一个列表(可变长度)并将其插入一个表(与每个列表中的值具有相同的列数):
ps.cur = psycopg_cursor
def load_lists(list_of_lists, table):
# Get number of columns in table
sql = """
SELECT column_name FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = '{}'
""".format(table)
ps.cur.execute(sql)
columns_list = [i[0] for i in ps.cur.fetchall()]
# Insert list of lists into table
columns = '(' + ','.join(columns_list) + ')'
parameters = '(' + ','.join(['%%s' for i in columns_list]) + ')'
for i in list_of_lists:
sql = """
INSERT INTO {} {}
VALUES {}
""".format(table, columns, parameters)
values = tuple([j for j in i])
ps.cur.execute(sql, values)
我得到以下回溯尝试执行函数:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "psyco.py", line 24, in load_lists
ps.cur.execute(sql, values)
psycopg2.ProgrammingError: syntax error at or near "%"
LINE 3: VALUES (%s,%s,%s,%s,%s)
^
看来Psycopg2无法将我的变量绑定到%s参数,但我无法弄清楚原因。非常感谢任何帮助或想法!
答案 0 :(得分:5)
当为我的查询构建参数标记时,我只需要在每个'前面有一个%。这有点令人困惑,因为Python中的旧字符串格式化方法也使用了%符号,但与Psycopg2不同。请参阅下面的工作代码(仅在'parameters'变量中更改):
ps.cur = psycopg_cursor
def load_lists(list_of_lists, table):
# Get number of columns in table
sql = """
SELECT column_name FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = '{}'
""".format(table)
ps.cur.execute(sql)
columns_list = [i[0] for i in ps.cur.fetchall()]
# Insert list of lists into table
columns = '(' + ','.join(columns_list) + ')'
parameters = '(' + ','.join(['%s' for i in columns_list]) + ')' # <--- THIS LINE
for i in list_of_lists:
sql = """
INSERT INTO {} {}
VALUES {}
""".format(table, columns, parameters)
values = tuple([j for j in i])
ps.cur.execute(sql, values)