我一直在尝试整理此代码以与我的数据库一起使用,但是无论我尝试什么,它似乎都不想插入。其他插入也有效,但我对这个没有任何运气。
id = str(5)
price = str(300)
a = datetime.date.today()
day = (a.strftime('%d/%m/%Y'))
day = str(day)
cursor.execute("insert into StockDate(StockID, Date, StockPrice) values (?, ?, ?)",
(id, day, price))
cnxn.commit()
我不断收到此错误:
('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement. (-3502) (SQLExecDirectW)')
我觉得这可能与日期有关,但老实说我没有太多线索。我的数据库日期格式为日/月/年(如果有帮助的话)。
最佳答案
“日期”是保留字。将列名称 Date
放在方括号中(如 [Date]
),以表明您将其用作名称:
cursor.execute("insert into StockDate(StockID, [Date], StockPrice) values (?, ?, ?)",
(id, day, price))
关于python - ('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement. (-3502) (SQLExecDirectW)'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53794941/