如何更新MS Access表,其中包含来自Pandas DF的自动数列



我需要使用PANDAS DF在MS访问中填充表。该表包括一个自动数列和另一列,默认值= now((。

我试图通过以下数据帧将ID和RowinSertedTime列作为空白,但是尚未解决。

d = {'ID': ['',''], 'col1': [1, 2], 'col2': [3, 4], 'RowInsertedTime': ['','']}
df = pd.DataFrame(data=d)

我使用PYODBC库执行了代码,如下所示:

connection_string = (
    r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
    r'DBQ=C:UsersPublicMyTest.accdb;'
)
cnxn = pyodbc.connect(connection_string, autocommit=True)
crsr = cnxn.cursor()
# insert the rows from the DataFrame into the Access table    
crsr.executemany(
    f"INSERT INTO [{table_name}] (ID, col1, col2, RowInsertedTime) VALUES (?, ?, ?, ?)",
    df.itertuples(index=False))

不幸的是,这返回以下错误:

DataError: ('22018', '[22018] [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression. (-3030) (SQLExecDirectW)')

知道我如何获得MS访问表中显示的空白字段的自动值?

您必须省略未更新的列。

crsr.executemany(
    f"INSERT INTO [{table_name}] (col1, col2) VALUES (?, ?)",
    df.itertuples(index=False))

访问将填充空白。

最新更新