SQL查询附加来自Python来访问

问题描述 投票:3回答:1

我想读取Excel文件转换为Python大熊猫的数据帧,然后将数据帧追加到预先存在的Access数据库。

我已经收到以下错误:

错误( 'HYC00', '[HYC00] [微软] [ODBC Microsoft Access驱动程序]可选功能未实现(106)(的SQLBindParameter)')

我已阅读,这个错误通常是pyodbc 4.0.25的结果。我有我的降级版本4.0.24 pyodbc,我仍然收到错误。

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()
python sql ms-access pyodbc
1个回答
1
投票

无需熊猫作为媒介。离开数据科学图书馆! MS访问与其兄弟,MS Excel的伟大工程。简单地询问工作簿没有任何循环纯SQL调用。

下面假定Excel中的数据保持相同的列名Access表在一个名为A1工作开始在细胞Sheet1。根据需要调整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()
© www.soinside.com 2019 - 2024. All rights reserved.