Psycopg2 --- 插入数组

Psycopg2 --- Inserting an array

我有一个变量数组 (arr),我想将其插入到我的数据库中,我在其中使用 Psycopg2。我插入的数组有 45 个条目。我现在 运行 的代码如下:

string = ''
for i in range(0, length):
    string = string + "%s, "
string = string[:-2]

query = """
    INSERT INTO schema.tablename
    VALUES (%s)"""
query = query.replace("%s", string)
cur.execute(query, (arr, ))

我想避免明确编写列,因为我正在使用的 table 可能会更改(添加 columns/remove 列)。上面的代码给我错误:

IndexError: tuple index out of range

我该怎么做?

你试过executemany功能了吗?

cur.executemany("INSERT INTO table VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)", tup)