[英]How can I Fix this SQL Syntax error 1064, Where It says to look at the MySQL server version for the right syntax to use?
[英]Can't get the right SQL syntax using pymsql, error 1064
我的輸出在csv中有效,但是在嘗試將其插入mysql時卻無效。 我收到以下錯誤,但無法解決。 我是新手,所以我可能缺少明顯的東西。 Python 2x和3x中的相同錯誤。
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key, title, content, start_date, end_date, initial_update) VALUES('reddit', 'h' at line 1")
mainDB_cnx = pymysql.connect(user='XXXX', password='XXXX',
host='XXXX',
database='Test', use_unicode=True, charset="utf8mb4")
with mainDB_cnx:
mainDB_cursor = mainDB_cnx.cursor()
mainDB_cursor.execute(
"INSERT INTO reddit(site, site_url, key, title, content, start_date, end_date, initial_update) VALUES(%s, %s, %s, %s, %s, STR_TO_DATE(%s,'%%Y-%%m-%%d'), STR_TO_DATE(%s,'%%Y-%%m-%%d'), STR_TO_DATE(%s,'%%Y-%%m-%%d'))",
(["reddit", "http://www.reddit.com", url, title, content, datetime.strptime(date,'%d %B %Y').strftime('%Y-%m-%d'), datetime.strptime('2018-07-25','%Y-%m-%d').strftime('%Y-%m-%d'), datetime.strptime('2018-07-25','%Y-%m-%d').strftime('%Y-%m-%d')]))
print("Successful")
KEY
是結構化查詢語言的MySQL方言中的保留字。 看到這個。 https://dev.mysql.com/doc/refman/8.0/zh-CN/keywords.html#keywords-8-0-detailed-K
因此,每當提及該列名稱時,都必須用定界符將該列名稱包裝起來。
嘗試
INSERT INTO reddit (side, site_url, `key`, title, ....
或者更好的是,不要在表中的列名中使用保留字。 下一個在您的系統上工作的程序員將感謝您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.