首页 > 解决方案 > SQL 查询的值部分中的占位符错误

问题描述

您好我正在尝试使用 python 从 excel 将数据导入访问数据库。

下面是我尝试过的代码

import xlrd
import pyodbc
# opening the excel sheets
incoming=xlrd.open_workbook(r'C:\Users\Desktop\Incoming.xlsx')
sheet_incoming=incoming.sheet_by_name('Incoming')
print(sheet_incoming)

# connecting to access database
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\Desktop\E CX DB.accdb;')
dbcurs=conn.cursor()



incoming_query = """INSERT INTO Incoming (SR_NO,SUP,ID,TYPE,SETUP_DATE,BU,Month_,Year_,RAF_IND)VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)"""
for r in range(1,sheet_incoming.nrows):
    SR_NO = sheet_incoming.cell(r,0).value
    SUP = sheet_incoming.cell(r,1).value
    ID = sheet_incoming.cell(r,2).value
    TYPE = sheet_incoming.cell(r,3).value
    SETUP_DATE = sheet_incoming.cell(r,4).value
    BU = sheet_incoming.cell(r,5).value
    Month_ = sheet_incoming.cell(r,6).value
    Year_ = sheet_incoming.cell(r,7).value
    RAF_IND = sheet_incoming.cell(r,8).value

    #assigning values
    values =(SR_NO,SUP,ID,TYPE,SETUP_DATE,BU,Month_,Year_,RAF_IND)

    # execute upload
    dbcurs.execute(incoming_query,values)
dbcurs.close()
conn.commit()
conn.close()

我得到的错误是Syntax error in query expression '%s'. (-3100) (SQLPrepare)")

问候,任。

标签: pythonpyodbc

解决方案


推荐阅读