我想通过 Python 将值列表插入到 Postgres 表中:
值在列表中说:
new=
[
[gold, dresden, 1000, 24],
[silver, Cologne, 600, 42],
[gold, Aachen, 3000, 25]
]
并且已经在 Postgres 中创建了带有标题的表。如何插入?
db.execute("INSERT INTO B4_O (position, city, amount, score) VALUES (%s,%s,%s)", new)
db.commit
但这给了我错误:
not all arguments converted during string formatting
最佳答案
使用psycopg2.extras.execute_values() :
new = [
['gold', 'dresden', 1000, 24],
['silver', 'Cologne', 600, 42],
['gold', 'Aachen', 3000, 25]
]
from psycopg2.extras import execute_values
execute_values(db, "INSERT INTO B4_O (position, city, amount, score) VALUES %s", new)
关于python - 向 postgres 表中插入一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51460590/