我在使用python連接sql修改表格的時間字段的時候,遇到這樣一個問題:
ProgrammingError: (pymysql.err.ProgrammingError) (1064, “You have an
error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use near
‘14:41:32.713604 WHERE table_id =‘1’’ at line 1”)
[SQL: UPDATE jd_shop_comparison1_new SET create_time =2025-07-09 14:41:32.713604
WHERE table_id =‘1’ ] (Background on this error at:
https://sqlalche.me/e/14/f405)
原代碼:
UPDATE_SQL = f"UPDATE table_name SET create_time ={create_time} WHERE table_id ='{table_id}' " #修改時間
engine.execute(UPDATE_SQL)
這個錯誤是由于 SQL 語句中的日期時間值沒有正確使用引號引起的。
在我的 UPDATE 語句中,日期時間值 create_time 沒有被引號包圍,導致 MariaDB 無法正確解析這個值。
解決方法:
使用字符串引號
包圍括號里的字符,包括日期、時間、文本等
例如:
UPDATE_SQL = f"UPDATE jd_shop_comparison1_new SET create_time = '{create_time}' WHERE table_id ='{table_id}' " #修改時間
engine.execute(UPDATE_SQL)
最終問題就解決了!
詳細的錯誤圖: