我有9个xlsx文件,我想从中读取数据,并将每一行添加到数据库的表中。
以下是代码:
import sqlalchemy
import os
import csv
import sqlite3
from readData import *
from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
#Change these
dbname = "TandWPODB2"
metadata = MetaData()
Base = declarative_base()
filePath = "CAM MIKE DATA\B2B\TEMPLE AND WEBSTER PO"
purchDirectory = os.path.abspath(os.path.join(filePath))
print purchDirectory
class TandWPurchaseOrder(Base):
__tablename__ = 'tandwpurchaseorder'
orderNum = Column(Integer)
barcode = Column(String)
skuNum = Column(String, primary_key=True)
shipID = Column(String)
description = Column(String)
quantity = Column(Integer)
costPrice = Column(String)
totalCost = Column(String)
agreedDeliveryDate = Column(String)
colour = Column(String)
material = Column(String)
itemDims = Column(String)
CoO = Column(String) # what even is this?
itemWeightEa = Column(String)
shipWidth = Column(String)
shipDepth = Column(String)
shipHeight = Column(String)
itemCubic = Column(String)
totalCubic = Column(String)
deadweight = Column(String)
def createDB(dbname):
global dbEngine
global dbSession
dbEngine = create_engine('sqlite:///' + dbname, echo=False)
#create Session
Session = sessionmaker()
Session.configure(bind=dbEngine)
dbSession = Session()
#create tables
Base.metadata.create_all(dbEngine)
print "db created"
def readPurchaseOrder(purchDirectory, session):
wsList = readFiles(purchDirectory)
wsRange = []
headers = []
# print type(wsList) #List
# print(type(wsList[0]))
#for i,ws in enumerate(wsList):
for ws in wsList:
#print type(ws)
list1, list2 = readWs(ws) # list1 = wsRange, list2 = headers
wsRange.append(list1)
headers.append(list2)
# print headers[0]
# print (wsRange)
#wsRange[i(ws)][j][k] = k item in row j of table/book i
# print(len(wsRange)) #9 = num pages
# print(len(wsRange[0])) #changing = num rows
# print(len(wsRange[0][0])) #20 = num item in row
# for i = len(wsRange)
for i in range(len(wsRange)):
for j in range(len(wsRange[i])):
TandWPurchOrder = TandWPurchaseOrder(
orderNum = wsRange[i][j][0].value,
barcode = wsRange[i][j][1].value,
skuNum = wsRange[i][j][2].value,
shipID = wsRange[i][j][3].value,
description = wsRange[i][j][4].value,
quantity = wsRange[i][j][5].value,
costPrice = wsRange[i][j][6].value,
totalCost = wsRange[i][j][7].value,
agreedDeliveryDate = wsRange[i][j][8].value,
colour = wsRange[i][j][9].value,
material = wsRange[i][j][10].value,
itemDims = wsRange[i][j][11].value,
CoO = wsRange[i][j][12].value, # what even is this?
itemWeightEa = wsRange[i][j][13].value,
shipWidth = wsRange[i][j][14].value,
shipDepth = wsRange[i][j][15].value,
shipHeight = wsRange[i][j][16].value,
itemCubic = wsRange[i][j][17].value,
totalCubic = wsRange[i][j][18].value,
deadweight = wsRange[i][j][19].value,
)
dbSession.add(TandWPurchOrder)
dbSession.commit()
createDB(dbname)
Base.metadata.create_all(dbEngine)
readPurchaseOrder(purchDirectory, dbSession)
我得到的错误是:
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) column skuNum is not unique [SQL: u'INSERT INTO tandwpurchaseorder
我对python和数据库很新,所以非常感谢任何帮助