插入列表到postgres表

我想通过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-psycopg2.InterfaceError:连接已关闭/ pgr_astar


下一篇:python-cursort.execute / cursor.callproc不返回错误,但不执行任何操作