python - 编程错误 : can't adapt type 'set'

标签 python excel postgresql

我正在使用 Python 将 Excel 数据导入 postgreSQL 并遇到编程错误。我确实研究了这个问题,发现它与 postgreSQL 有关。有人可以提供帮助吗。

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'

最佳答案

您的查询似乎需要所有字符串:

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)"""

在你的值(value)观中几乎只有字符串,但一个外星人脱颖而出:

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)

(因为不需要把Address放在一个set中,所以看起来和其他字段一样是一个字符串)

关于python - 编程错误 : can't adapt type 'set' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40347497/

相关文章:

python - 使用 "=="比较数字与零或将数字与 bool 值比较之间是否有很大差异?

python - 每当我输入某些命令时,我的机器人就会重复自己(discord.py,Python v3.7)

excel - 有什么方法可以在excel宏中保存一行以供将来使用?

java - 我可以通过这种方式传递列名吗?

postgresql - Postgres : authentication fails when try to login with different unix user

python - google app engine 将大数据放入数据存储区的有效方法

python - 如何使用占位符分配 tf.Variables?

excel - 扩展 Excel MATCH 函数

mysql - 在 Mac Excel 2011 中从 Mysql DB 获取数据的 VBA 代码

ruby-on-rails - Rails ActiveRecord,在 Postgres 中插入默认字段值