2
votes

I have been trying to sort out this code to work with my database, however no matter what I've tried it doesn't seem to want to insert. Other inserts have worked, but I haven't had any luck with this one.

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()

I keep getting this error:

('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement. (-3502) (SQLExecDirectW)')

I feel like it might be something to do with the date, but I honestly don't have much of a clue. My database the date is formatted as day/month/year if that helps at all.

1

1 Answers

2
votes

“Date” is a reserved word. Put the column name Date in square brackets (as in [Date]) to indicate that you are using it as a name:

cursor.execute("insert into StockDate(StockID, [Date], StockPrice) values (?, ?, ?)", 
(id, day, price))