【发布时间】:2019-04-29 14:30:44
【问题描述】:
我需要使用 pandas df 在 MS Access 中填充一个表。此表包括一个自动编号列和另一个默认值为 =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:\Users\Public\MyTest.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 Access 表中显示的空白字段的自动值吗?
【问题讨论】: