简体   繁体   中英

Transfering Data in MS Access Using Python

I have an ever growing and changing database that reflects a permits passed by the State and EPA. As the database changes and updates I need to transfer the relevant information.

The script does two things; first it checks which fields are the same and creates a list of fields and data that will be inserted into the new database. Second to insert the data into the new database.

Problem is I cannot get it to insert. I have matched everything like it says online in various ways but i get error ('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement. (-3502) (SQLExecDirectW)').

I cannot figure out how to prevent it.

Code:

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()

This is the sql string I create

"INSERT INTO tbl_Outfall_1_Profile ([profile_OutfallName], [profile_HistoricalName1], [profile_HistoricalName2], [profile_HistoricalName3], [profile_HistoricalName4]) Values ('756', '', '', '', '');"

Taking what @Gord Thompson said I was actually able to create a dynamic parameter flow

First created a module to create the ?

def Defining_Paramters(length):
    parameterString = ""
for x in range(1,length):
    parameterString += "?, "
parameterString += "?"

return parameterString

Then stuck it into the string for the sql update

sqlUpdate = sqlUpdate = "INSERT INTO %s (%s) Values (%s);" %(table, sqlFrameworkSubStr, parameters)

Run the cursor and commit it

cursorTo.execute(sqlUpdate, (dataTuple))
connectionTo.commit()

It would seem that you have to create the query in its entirety then have your data in tuple format for entry

This is the sql string [I think] I create

Try this:

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

or perhaps:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM