2016-10-31 21 views
1

我正在使用Python將Excel數據導入到PostgreSQL中,並且運行時出現編程錯誤。我研究了這個問題,發現它與postgreSQL有關。有人可以提供幫助嗎?ProgrammingError:無法改寫'set'

import psycopg2 
import xlrd 

book = xlrd.open_workbook("T:\data.xlsx") 
sheet = book.sheet_by_name("HCSData") 
database = psycopg2.connect (database = "", user="") 

cursor = database.cursor() 
delete = """Drop table if exists "Python".hcsdata""" 
print (delete) 
mydata = cursor.execute(delete) 

cursor.execute('''CREATE TABLE "Python".hcsdata 
    (DCAD_Prop_ID varchar(55), 
    Address VARCHAR(50), 
    Addition   VARCHAR(100), 
    Block text, 
    Lot integer, 
    Permit_Num   varchar(55), 
    Permit_Date    date, 
    Foundation_Date date, 
    Frame_Date date, 
    Final_Date date, 
    HCS  varchar(55), 
    Project_ID integer 
    );''') 
print "Table created successfully" 

query = """INSERT INTO "Python".hcsdata (DCAD_Prop_ID,Address,Addition, Block, Lot, 
Permit_Num,Permit_Date, Foundation_Date, Frame_Date, Final_Date, HCS,Project_ID) 
VALUES (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s)""" 

for r in range(1, sheet.nrows): 
    DCAD_Prop_ID = sheet.cell(r,0).value 
    Address = sheet.cell(r,1).value 
    Addition = sheet.cell(r,2).value 
    Block = sheet.cell(r,3).value 
    Lot = sheet.cell(r,4).value 
    Permit_Num = sheet.cell(r,5).value 
    Permit_Date = None if not sheet.cell(r,6).value else xlrd.xldate.xldate_as_datetime(sheet.cell(r,6).value,book.datemode) 
    Foundation_Date = None if not sheet.cell(r,7).value else xlrd.xldate.xldate_as_datetime(sheet.cell(r,7).value,book.datemode) 
    Frame_Date = None if not sheet.cell(r,8).value else xlrd.xldate.xldate_as_datetime(sheet.cell(r,8).value,book.datemode) 
    Final_Date = None if not sheet.cell(r,9).value else xlrd.xldate.xldate_as_datetime(sheet.cell(r,9).value,book.datemode) 
    HCS = sheet.cell(r,10).value 
    Project_ID =sheet.cell(r,11).value 

    values = (DCAD_Prop_ID,set(Address),Addition, Block, Lot, 
Permit_Num,Permit_Date, Foundation_Date, Frame_Date, Final_Date, HCS,Project_ID) 

    cursor.execute(query, values) 

cursor.close() 

database.commit() 

database.close() 

print "" 
print "All Done! Bye, for now." 
print "" 
columns = str(sheet.ncols) 
rows = str(sheet.nrows) 
print "Done" 

和錯誤的Python顯示的是:

Drop table if exists "Python".hcsdata 
Table created successfully 

Traceback (most recent call last): 
    File "C:\Users\Programming\PythonSQLNew\HCSData.py", line 53, in <module> 
cursor.execute(query, values) 
ProgrammingError: can't adapt type 'set' 
+1

是你確定'set(Address)'?如果你需要unicity,你應該做'list(set(Address))'。但我懷疑它應該只是「地址」。 –

+0

感謝Jean,你認爲錯誤是在Address嗎? –

+1

我很確定。嘗試傳遞別的東西。我會那樣做。 –

回答

2

查詢似乎期望所有的字符串:

query = """INSERT INTO "Python".hcsdata (DCAD_Prop_ID,Address,Addition, Block, Lot, 
Permit_Num,Permit_Date, Foundation_Date, Frame_Date, Final_Date, HCS,Project_ID) 
VALUES (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s)""" 

在你的價值觀有幾乎只有字符串的僑民脫穎而出:

values = (DCAD_Prop_ID,set(Address),Addition, Block, Lot, 
Permit_Num,Permit_Date, Foundation_Date, Frame_Date, Final_Date, HCS,Project_ID) 

我承認該消息是神祕的,但邏輯告訴我們只是做:

values = (DCAD_Prop_ID,Address,Addition, Block, Lot, 
Permit_Num,Permit_Date, Foundation_Date, Frame_Date, Final_Date, HCS,Project_ID) 

(因爲沒有必要把Addressset,它似乎是像其他領域的字符串)

相關問題