使用python在ms access中传输数据

lbsnaicq  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(279)

我有一个不断增长和变化的数据库,它反映了州和环保局通过的许可证。随着数据库的更改和更新,我需要传输相关信息。
剧本做了两件事;首先,它检查哪些字段是相同的,并创建将插入新数据库的字段和数据列表。第二步,将数据插入新数据库。
问题是我无法将其插入。我以各种方式匹配了它在网上所说的所有内容,但在insert into语句中出现了错误('42000','[42000][microsoft][odbc microsoft access driver]语法错误(-3502)(sqlexecdirectw)')。
我想不出如何预防它。
代码:

import pyodbc

importDatabase = r"J:\ENVIRO FIELD\AccessDatabases\MS4\MS4 Town Databases\~Template\MS4_Apocalypse Import DEV 1.accdb"

"Create the Import Database Connection"
connectionImport = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s;' %(importDatabase))
cursorImport = connectionImport.cursor()

"####---Outfall Section---####"

"Import the outfall names into the new database"

tbl = "tbl_Outfall_1_Profile"

exportList = []
importList = []
for row in cursorImport.columns(table = "tblExportMigration_Outfall_1_Profile"):
    field = row.column_name
    exportList.append(field)

for row in cursorImport.columns(table = "tbl_Outfall_1_Profile"):
    field = row.column_name
    importList.append(field)

matchingList = []
for field in exportList:
    if field != "outfallID":
        if field in importList:
            matchingList.append(field)
    else:
        continue

sqlValue = ""
for field in matchingList:
    sqlValue += "[%s], " %(field)

sqlValue = sqlValue[:-2]
sql = "SELECT %s from %s" %(sqlValue, "tblExportMigration_Outfall_1_Profile")

for rowA in cursorImport.execute(sql):
    tupleList = list(rowA)
    tupleList = ["" if i == None else i for i in tupleList]
    tupleValues = tuple(tupleList)
    sqlUpdate = """INSERT INTO tbl_Outfall_1_Profile (%s) Values %s;""" %(sqlValue, tupleValues)
    cursorImport.execute(sqlUpdate)

cursorImport.close()

这是我创建的sql字符串
“将值('756','','','','')插入tbl_排水口_1_剖面([profile_OutpallName]、[profile_historicalname1]、[profile_historicalname2]、[profile_historicalname3]、[profile_historicalname4])

brtdzjyr

brtdzjyr1#

这是我创建的sql字符串
试试这个:

sqlUpdate = """INSERT INTO tbl_Outfall_1_Profile (%s) Values (%s);""" %(sqlValue, tupleValues)

或许:

sqlUpdate = "INSERT INTO tbl_Outfall_1_Profile (%s) Values (%s);" %(sqlValue, tupleValues)

相关问题