其他分享
首页 > 其他分享> > 插入列表到postgres表

插入列表到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)

标签:postgresql,psycopg2,python
来源: https://codeday.me/bug/20191108/2009801.html