I want to insert data from a CSV file into a PostgreSQL table. The table
結構如下。但是我無法給出INTEGER類型值的輸入。 它表示錯誤喜歡 - DataError:無效輸入語法整數: 「vendor_phone」 LINE 1:... vendor_phone,vendor_address)VALUES( 'VENDOR_NAME','vendor_ph ...從CSV文件插入到整數值類型的PostgreSQL表
它是工作正常,如果我用VARCHAR類型,但我需要使用整數值。
CREATE TABLE vendors (
vendor_id SERIAL PRIMARY KEY,
vendor_name VARCHAR(100) NOT NULL,
vendor_phone INTEGER,
vendor_address VARCHAR(255) NOT NULL
)
import psycopg2
import csv
database = psycopg2.connect (database = "supplier", user="postgres", password="1234", host="localhost", port="5432")
光標= database.cursor()
vendor_data = csv.reader(open('vendors.csv'),delimiter=',')
for row in vendor_data:
cursor.execute("INSERT INTO vendors (vendor_name,vendor_phone,vendor_address)"\
"VALUES (%s,%s,%s)",
row)
print("CSV data imported")
cursor.close()
database.commit()
database.close()
看仔細錯誤信息,它說你要插入單詞「 vendor_phone「,這顯然不是一個整數。這聽起來像你有一個標題行,你需要跳過。 – IMSoP
請參閱此鏈接..可能對您有所幫助... (1)https://stackoverflow.com/questions/2987433/how-to-import-csv-file-data-into-a-postgresql-table (2)https://stackoverflow.com/questions/19400173/how-should-i-import-data-from-csv-into-a-postgres-table-using-pgadmin-3 –
我需要輸入整數值....此外,我有一些我的代碼 - header = next(vendor_data) rows = [header] + [[row [0],int(row [1]),row [2] (供應商名稱,vendor_phone,vendor_address)「 」VALUES(%s,%??,%s)「, row) – Neepa