我正在嘗試postgresql從dataframe這樣的資料中插入資料
df["code"] = df["code"].astype(int)
insert_sql = '''
INSERT INTO cs_houmon_ (code, name, city, add, phone)
VALUES (%s, %s, %s, %s, %s)
ON CONFLICT ON CONSTRAINT code DO NOTHING;
'''
try:
with conn.cursor() as cur:
cur.execute(insert_sql, (df.code.to_list(), df.name.to_list(), df.city.to_list(), df.add.to_list(), df.phone.to_list()))
conn.commit()
cursor.close()
except Exception as e:
log(str(e))
但是得到這個錯誤代碼
code is of type integer but expression is of type integer[]
LINE 3: VALUES (ARRAY[1,2,3,4,5,6,7,8,9,10,11,12,13,14,1...
我的sql表是這樣創建的
CREATE TABLE cs_houmon_ (code integer, ...
uj5u.com熱心網友回復:
錯誤說明了問題所在 - 程式需要整數,您在那里傳遞整數串列。
這是因為execute用于插入單行,作為第二個引數,您應該有長度為 5 的單元組。如果要插入多個引數,請使用executemany并傳遞元組串列
cur.executemany(insert_sql, list(zip(l1, l2, l3, l4, l5))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/389914.html
標籤:Python sql 蟒蛇-3.x PostgreSQL的
下一篇:兩個時間段內出現的IDS數量不同
