Skip to content
Advertisement

Inserting rows from df to MS Access Error “number of query values and destination fields are not the same”

I have a dataframe called df2 which has the following columns:

JavaScript

I count this as 8 columns.

I have saved the column names into cols:

JavaScript

Again, I’m seeing 8 columns.

And, in Access I have a table that has these columns (none of which are assigned as a primary key, for now):

JavaScript

which is 8 columns. I tried to execute this code to populate test_table with the data from df2:

JavaScript

but I am getting this error:

JavaScript

I was thinking it was because it is trying to insert the index, and did index_col = 0 when reading the csv file (which assigned to df2). However, that decreased the column count by 1, and I adjusted the cols values and VALUES in the sql statement accordingly, but I came up with the same error.

Any ideas? TIA.

EDIT I thought it might be a data types issue. I changed all the data types in Access to ‘short text’ and the datatype in df2 to object. Still the same error.

Advertisement

Answer

In MS Access, you need to square bracket or backtick table or column names with spaces and/or special characters. Right now, you use single quotes in your str.join but then use backticks in the SQL statement. Simply, use backticks or square brackets in both strings:

JavaScript

Consider even executemany:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement