创建MySql表格的时候,顺利建表成功,但是在插入数据时却报了如下错误:
_mysql_exceptions.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 'current_date,knowledge_level,grade_group_id,subject_id,current_arrive_level,curr' at line 1")
下面将详细给出本文的解决方案过程:
给出报错-> 打印出报错的sql语句->命令行执行->
(1)下面是具体报错的情况:
File "core_compute_stu_master_ratio.py", line 862, in <module>
obj_Compute_stu_master_ratio.run_main_compute_star("高中","化学")
File "core_compute_stu_master_ratio.py", line 854, in run_main_compute_star
self.computeStu_Star_for_each_Knowledge_node_and_direct_write_to_database("final_Stu_arrive_Kh_Star_result.txt",map_kn_id_to_its_level,map_stu_id_to_its_dati_reccords_for_each_kn,map_kn_id_to_its_name,map_stu_id_to_its_info,map_kn_id_to_its_auto_knowledge_id)
File "core_compute_stu_master_ratio.py", line 788, in computeStu_Star_for_each_Knowledge_node_and_direct_write_to_database
self.execute_kg(sql_insert_stu_arrive_star)
File "core_compute_stu_master_ratio.py", line 49, in execute_kg
return self.conn_kg.execute(sql)
File "/home/yyz/809/db.py", line 33, in execute
return self.cursor.execute(sql)
File "/usr/local/python2.7/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/usr/local/python2.7/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.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 'current_date,knowledge_level,grade_group_id,subject_id,current_arrive_level,curr' at line 1")
(2) 打印报错sql语句 很明显,错误提示是mysql的插入语句出错了,这时你应该把这个mysql 语句打印出来,然后判断其是否出错。
下面是程序打印结果
replace into students_arrive_knowledge_level_for_master_ratio_new(student_id,knowledge_id,current_date,knowledge_level,grade_group_id,subject_id,current_arrive_level,current_score,next_arrive_level,next_score,ctime,utime,moji_knowledge_node_id,total_number) values('113356','6oo7ryi0e79zkqai08wbmeohxn6c022f','2017-08-09','3','3','5','1','0.900000','2','0.800000','2017-08-09 15:28:09','2017-08-09 15:28:09','1200174','10')
(3)命令行执行:用眼看不出来错误,接着,下一个大招是在mysql命令行下,再次这行打印出来的这条mysql命令,但不幸的是,错误依然存在
mysql> replace into students_arrive_knowledge_level_for_master_ratio_new(student_id,knowledge_id,current_date,knowledge_level,grade_group_id,subject_id,current_arrive_level,current_score,next_arrive_level,next_score,ctime,utime,moji_knowledge_node_id,total_number) values('113356','6oo7ryi0e79zkqai08wbmeohxn6c022f','2017-08-09','3','3','5','1','0.900000','2','0.800000','2017-08-09 15:18:41','2017-08-09 15:18:41','1200174','10')
-> ;
ERROR 1064 (42000): 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 'current_date,knowledge_level,grade_group_id,subject_id,current_arrive_level,curr' at line 1
(4)解决之道: 苦思冥想之际,发觉问老司机才是解决之捷径,遂问之,他给出如下两个步骤:
第一,根据出错原因,将出错字段添加倒引号(一般在你键盘左上角的esc下面那个),添加之后,在mysql命令行之下执行成功
mysql> replace into students_arrive_knowledge_level_for_master_ratio_new(student_id,knowledge_id,`current_date`,knowledge_level,grade_group_id,subject_id,current_arrive_level,current_score,next_arrive_level,next_score,ctime,utime,moji_knowledge_node_id,total_number) values('113356','6oo7ryi0e79zkqai08wbmeohxn6c022f','2017-08-09','3','3','5','1','0.900000','2','0.800000','2017-08-09 15:18:41','2017-08-09 15:18:41','1200174','10');
Query OK, 1 row affected (0.01 sec)
第二,接着老司机给出了原因,原来是我在定义和创建表格的字段时误用了mysql的关键字,瞬间醒悟,非常,万分感谢老司机!
第三,百度之下,果不其然,current_date是对应的MySQL中内置的关键字,MySQL内置关键字参见 http://www.cnblogs.com/*fu/p/5949764.html
第四,解决策略有两个:第一,修改该表格中的字段名,改成不是mysql的内置关键字
第二,继续使用该表格定义,在插入数据时,对该关键字两边添加倒引号