我正在尝试将Excel文件读入Python熊猫数据框,然后将数据框附加到预先存在的Access数据库中。
我一直收到以下错误:
错误(“ HYC00”,“ [HYC00] [Microsoft] [ODBC Microsoft Access 驱动程序]未实现可选功能(106)(SQLBindParameter)')
我已阅读到此错误通常是由于pyodbc 4.0.25导致的。我已将我的版本降级为pyodbc 4.0.24,但仍收到错误消息。
import glob
import os
import pandas as pd
import time
import pyodbc
# specify the folder that the files are sitting in
list_of_files = glob.glob(r'C:\Users\CraigG\Test\*xlsx')
# define the newest file in the folder
filename = min(list_of_files, key=os.path.getmtime)
# put excel file in data frame
df = pd.read_excel(filename)
print(pyodbc.version)
conn = pyodbc.connect(r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};' +
r'DBQ=C:\\Users\\CraigG\\Test\\Db\\Master.accdb;')
cursor = conn.cursor()
for index, row in df.iterrows():
AccountNum = row["Account #"]
CustomerNum = row["Customer #"]
CustomerName = row["Customer Name"]
AccountName = row["Account Name"]
SalesRegisterNum = row["Sales Register #"]
InvoiceDate = row["Invoice Date"]
BillingMonthDate = row["BillingMonthDate"]
WrittenBy = row["Written By"]
Mfr = row["Mfr"]
CatalogNo = row["CatalogNo"]
LineType = row["LineType"]
UPC = row["UPC"]
QTYShip = row["QTY Ship"]
Price = row["Price"]
PriceUOM = row["Price UOM"]
ExtenedPrice = row["Extened Price"]
Cost = row["Cost"]
CostUOM = row["Cost UOM"]
ExtendedCost = row["Extended Cost"]
SPACost = row["SPA Cost"]
SPACostUOM = row["SPA Cost UOM"]
ExtendedSPACost = row["Extended SPA Cost"]
PCNum = row["PC #"]
sql = "INSERT INTO Master ([Account #], [Customer #], [Customer Name], [Account Name], [Sales Register #], [Invoice Date], [BillingMonthDate], [Written By], [Mfr], [CatalogNo], [LineType], [UPC], [QTY Ship], [Price], [Price UOM], [Extened Price], [Cost], [Cost UOM], [Extended Cost], [SPA Cost], [SPA Cost UOM], [Extended SPA Cost], [PC #]) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
params = (AccountNum, CustomerNum, CustomerName, AccountName,
SalesRegisterNum, InvoiceDate, BillingMonthDate, WrittenBy, Mfr,
CatalogNo, LineType, UPC, QTYShip, Price, PriceUOM, ExtenedPrice, Cost,
CostUOM, ExtendedCost, SPACost, SPACostUOM, ExtendedSPACost, PCNum)
cursor.execute(sql, params)
cursor.commit()
答案 0 :(得分:1)
不需要熊猫作为媒介。离开图书馆进行数据科学研究! MS Access与其同级MS Excel配合使用非常出色。只需在工作簿中查询纯SQL调用即可,而无需任何循环。
以下假定Excel数据与名为A1
的工作表中的单元格Sheet1
中的Access表保持相同的列名。根据需要调整FROM
子句。
import glob
import os
import pyodbc
# specify the folder that the files are sitting in
list_of_files = glob.glob(r'C:\Users\CraigG\Test\*xlsx')
# define the newest file in the folder
filename = min(list_of_files, key=os.path.getmtime)
sql = r"""INSERT INTO Master ([Account #], [Customer #], [Customer Name], [Account Name], [Sales Register #],
[Invoice Date], [BillingMonthDate], [Written By], [Mfr], [CatalogNo], [LineType],
[UPC], [QTY Ship], [Price], [Price UOM], [Extened Price], [Cost], [Cost UOM],
[Extended Cost], [SPA Cost], [SPA Cost UOM], [Extended SPA Cost], [PC #])
SELECT e.[Account #], e.[Customer #], e.[Customer Name], e.[Account Name], e.[Sales Register #],
e.[Invoice Date], e.[BillingMonthDate], e.[Written By], e.[Mfr], e.[CatalogNo], e.[LineType],
e.[UPC], e.[QTY Ship], e.[Price], e.[Price UOM], e.[Extened Price], e.[Cost], e.[Cost UOM],
e.[Extended Cost], e.[SPA Cost], e.[SPA Cost UOM], e.[Extended SPA Cost], e.[PC #]
FROM [Excel 12.0 Xml;HDR=Yes;Database={xl}].[Sheet1$] AS e;
""".format(xl=filename)
conn = pyodbc.connect(r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};' +
r'DBQ=C:\Users\CraigG\Test\Db\Master.accdb;')
cursor = conn.cursor()
cursor.execute(sql)
cursor.commit()
cursor.close()
conn.close()