Pyodbc: Issue inserting into MS Access database

百般思念 提交于 2019-12-20 07:13:06

问题


I'm trying to automate data collection from the results of an online form, compile them into an Excel file, and then upload the data from the Excel file into Access. I have a Python script that does this for another database and works perfectly fine, but the same code does not work for the new database for some reason.

Using pydobc, I'm able to successfully connect to the database and perform simple SELECT and INSERT statements such as:

DBcursor.execute("insert into Table1(Field1, Field2) values(?, ?)", ('Test 3', 'Test 4'))

This is properly inserted into the database and throws no exception. However, the following code does not:

    sql = "insert into [Wufoo Form Data]("      # Start of string
    for c in columns:                           # Concatenate all column names in string
        if c == columns[len(columns)-1]:        # for valid insert statement
            sql += c
        else:
            sql += c + ", "

    sql += ") values(" + values + ")"        # Insert string of ?s equal to number of
                                             # values in insert statement (final variable) `

    errors = 0
    for row in excelResults:                    # Insert data row by row
        #try:                                   # Try block to skip duplicate keys
                                                # and count number of invalid entries

            DBcursor.execute(sql, row)          # ERROR OCCURS HERE

        #except:
            errors += 1                         # If key already exists, add one to error
    DBconn.commit()                             # Commit all SQL statements in DBcursor  
    return errors                               # Return number of errors for suer feedback

I use a for loop to concatenate values together to create my sql insert string. When the code gets to DBcursor.execute(sql, row), these are the following values:

sql = "insert into [Wufoo Form Data]([Entry Id], [First Name], [Last Name], [ID Number], Email, Employee, Student, [Start Date], [End Date], [Signed First], [Signed Last], [Today's Date], [Date Created], [Created By], [Last Updated], [Updated By], [IP Address], [Last Page Accessed], [Completion Status]) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"

row = ('1', 'Test', 'Form', '1234567', 'email@server.com', 'Access (on ID card)', None, '2019-07-31', '2019-08-15', 'Test', 'Form', '2019-08-01 17:58:05', 'public', None, None, None, None, None, None)

However, this is the error I get: ('The SQL contains 0 parameter markers, but 19 parameters were supplied', 'HY000') which I would understand if I hadn't used the ?s to denote the values to be inserted.

My other script uses the exact same code but inserts data into the database correctly. I have a feeling there has to be something wrong with my syntax, but I've been looking over it for the better part of a week with no luck. I'm hoping a fresh set of eyes will help.

Thanks in advance!

来源:https://stackoverflow.com/questions/57402818/pyodbc-issue-inserting-into-ms-access-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!