当前位置:   article > 正文

pymysql.err.ProgrammingError: (1064, “You have an error in your SQL syntax; check the manual that co_pymysql.err.programmingerror: (1064, "you have an

pymysql.err.programmingerror: (1064, "you have an error in your sql syntax;

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 'CREATE TABLE `sinatop20` ( `snid` int(11) NOT NULL AUTO_INCREMENT, `id` varchar(' at line 1")

对于报错解析:很明显这是语法错误,我上看下看,左看右看,都没有看出问题来。

在pycharm中执行报语法错误,但是我复制到mysql数据库去执行又是正常的。就很纳闷。

目录

一、执行的语句

二、分析

1、 在pycharm运行就报错了:

 2、在mysql运行是正常的

3、错误寻找

4、答案在这:


一、执行的语句

原来的执行语句的功能:"""如果存在表就卸载表;然后创建表"""

DROP TABLE IF EXISTS `sinatop20`; CREATE TABLE `sinatop20` ( `snid` int(11) NOT NULL AUTO_INCREMENT, `id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `media` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `author` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `comment_url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `create_date` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `create_time` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `cat_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `top_time` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `top_num` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext3` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext4` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext5` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `time` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,  PRIMARY KEY (`snid`) USING BTREE ) ENGINE = MyISAM AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS `sinatop20`; CREATE TABLE `sinatop20` ( `snid` int(11) NOT NULL AUTO_INCREMENT, `id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `media` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `author` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `comment_url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `create_date` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `create_time` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `cat_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `top_time` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `top_num` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext3` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext4` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext5` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `time` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,  PRIMARY KEY (`snid`) USING BTREE ) ENGINE = MyISAM AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

二、分析

1、 在pycharm运行就报错了:

  1. Traceback (most recent call last):
  2. File "E:\WorkSpace\GraPro\DISHSASysBTM_ManageSys\DataCrawl\sinaTop20.py", line 62, in <module>
  3. cur.execute(create_sql)
  4. File "C:\Programs\Python\Python310\lib\site-packages\pymysql\cursors.py", line 170, in execute
  5. result = self._query(query)
  6. File "C:\Programs\Python\Python310\lib\site-packages\pymysql\cursors.py", line 328, in _query
  7. conn.query(q)
  8. File "C:\Programs\Python\Python310\lib\site-packages\pymysql\connections.py", line 517, in query
  9. self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  10. File "C:\Programs\Python\Python310\lib\site-packages\pymysql\connections.py", line 732, in _read_query_result
  11. result.read()
  12. File "C:\Programs\Python\Python310\lib\site-packages\pymysql\connections.py", line 1075, in read
  13. first_packet = self.connection._read_packet()
  14. File "C:\Programs\Python\Python310\lib\site-packages\pymysql\connections.py", line 684, in _read_packet
  15. packet.check_error()
  16. File "C:\Programs\Python\Python310\lib\site-packages\pymysql\protocol.py", line 220, in check_error
  17. err.raise_mysql_exception(self._data)
  18. File "C:\Programs\Python\Python310\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
  19. raise errorclass(errno, errval)
  20. 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 'CREATE TABLE `sinatop20` ( `snid` int(11) NOT NULL AUTO_INCREMENT, `id` varchar(' at line 1")
  21. Process finished with exit code 1

 2、在mysql运行是正常的

  1. DROP TABLE IF EXISTS `sinatop20`
  2. > OK
  3. > 时间: 0.001s
  4. CREATE TABLE `sinatop20` ( snid int(11) NOT NULL AUTO_INCREMENT, `id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `media` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `author` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `comment_url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `create_date` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `create_time` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `cat_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `top_time` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `top_num` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext3` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext4` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `ext5` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `time` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`snid`) USING BTREE ) ENGINE = MyISAM AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic
  5. > OK
  6. > 时间: 0.003s

3、错误寻找

找了很多,有人说是使用的字段有可能是关键字,要用 `` 把关键字包起来,我也包起来了,其他什么逗号,括号都没有问题。毕竟在mysql查询里可以运行,那就语句是没有问题的。

然后想起昨晚第一次运行都没有问题,后来我是改了哪里出了问题?

4、答案在这:

因为在pycharm里,上述sql语句中,我同时执行了两个命令!!!

要把DROP和CREATE分开两次运行,不能放在一个excute执行!!!

我的代码在这:

-------报错的代码示例:

 -----解决错误后代码示例:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/508934
推荐阅读
相关标签
  

闽ICP备14008679号