我有一個包含幾列的 csv 檔案:
upc date quantity customer
在我的physical表中,id每行都有一個自動生成列:
id upc date quantity customer
upc當我運行 python 腳本復制到 db 時,db 似乎將 解釋為實際 id。我收到此錯誤訊息:
Error: value "1111111" is out of range for type integer
CONTEXT: COPY physical, line 1, column id: "1111111"
我以前從未嘗試過,但我相信這是正確的:
def insert_csv(f, table):
connection = get_postgres_connection()
cursor = connection.cursor()
try:
cursor.copy_from(f, table, sep=',')
connection.commit()
return True
except (psycopg2.Error) as e:
print(e)
return False
finally:
cursor.close()
connection.close()
我在這里做錯了什么,還是必須創建另一個腳本才能從表中獲取最后一個 id?
更新的作業代碼:
def insert_csv(f, table, columns):
connection = get_postgres_connection()
cursor = connection.cursor()
try:
column_names = ','.join(columns)
query = f'''
COPY {table}({column_names})
FROM STDOUT (FORMAT CSV)
'''
cursor.copy_expert(query, f)
connection.commit()
return True
except (psycopg2.Error) as e:
print(e)
return False
finally:
cursor.close()
connection.close()
columns = (
"upc",
"date_thru",
"transaction_type",
"transaction_type_subtype",
"country_code",
"customer",
"quantity",
"income_gross",
"fm_serial",
"date_usage"
)
with open(dump_file, 'r', newline='', encoding="ISO-8859-1") as f:
inserted = insert_csv(f, 'physical', columns)
uj5u.com熱心網友回復:
您需要指定要匯入的列。從檔案:
列- 可迭代的列名以匯入。長度和型別應與要讀取的檔案內容相匹配。如果未指定,則假定整個表與檔案結構匹配。
您的代碼可能如下所示:
def insert_csv(f, table, columns):
connection = connect()
cursor = connection.cursor()
try:
cursor.copy_from(f, table, sep=',', columns=columns)
connection.commit()
return True
except (psycopg2.Error) as e:
print(e)
return False
finally:
cursor.close()
connection.close()
with open("path_to_my_csv") as file:
insert_csv(file, "my_table", ("upc", "date", "quantity", "customer"))
如果您必須以如下方式使用copy_expert()修改您的功能:
def insert_csv(f, table, columns):
connection = connect()
cursor = connection.cursor()
try:
column_names = ','.join(columns)
copy_cmd = f"copy {table}({column_names}) from stdout (format csv)"
cursor.copy_expert(copy_cmd, f)
connection.commit()
return True
except (psycopg2.Error) as e:
print(e)
return False
finally:
cursor.close()
connection.close()
uj5u.com熱心網友回復:
從這里復制:
如果指定了列串列,則 COPY TO 僅將指定列中的資料復制到檔案中。對于 COPY FROM,檔案中的每個欄位都按順序插入到指定的列中。未在 COPY FROM 列串列中指定的表列將接收其默認值。
因此 CSV 檔案中的值將從左到右分配,表格末尾的欄位將獲得它們的DEFAULT值。如果您不想發生這種情況,那么從這里copy_from:
列 - 可迭代的列名以匯入。長度和型別應與要讀取的檔案內容相匹配。如果未指定,則假定整個表與檔案結構匹配。
創建與檔案結構匹配的列串列,省略id將填充sequence值的列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/372806.html
標籤:Python PostgreSQL的 文件 复制 psycopg2
下一篇:圖表的下拉選單
