MySQL的1064错误是SQL语句写的有问题时出现的,即SQL的语法错误。笔者常常使用MySQL-python这个库来对MySQL进行操作,代码中报这个错误的一般是cursor.execute(sql, param)这一行。

这种参数式执行SQL语句的用法可以有效防止SQL注入的安全问题,但是为什么MySQL会报错呢?如果你确认SQL写的没问题,检查一下SQL语句中是否使用了引号。

在使用cursor.execute(sql, param)时,MySQL-python库会自动转义含有%s的字符串,所以不要画蛇添足在SQL语句中给%s加引号了,会报1064的错误滴!

另外也有许多人使用有SQL注入隐患的cursor.execute(sql % param)这种用法,这样是可以给%s加引号的。

但是安全问题孰重孰轻,相信各位自有判断。


在使用pymysql对mysql进行操作时,使用%s给excute传入参数时出错,错误代码如下:

table="huxing_table"
key="house_structure_page_url"
value="test"
cursor=db.cursor()
cursor.execute("INSERT INTO %s (%s) VALUES(%s)",(table,key,value))
db.commit()
cursor.close()

错误提示为:

Traceback (most recent call last):File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/cursors.py", line 112, in executeresult = self._query(query)File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/cursors.py", line 230, in _queryconn.query(q)File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/connections.py", line 607, in queryself._affected_rows = self._read_query_result()File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/connections.py", line 691, in _read_query_resultresult.read()File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/connections.py", line 869, in readself.first_packet = self.connection.read_packet()File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/connections.py", line 686, in read_packetpacket.check_error()File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/connections.py", line 328, in check_errorraise_mysql_exception(self.__data)File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/err.py", line 142, in raise_mysql_exception_check_mysql_exception(errinfo)File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/err.py", line 135, in _check_mysql_exceptionraise errorclass(errno,errorvalue)
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 ''huxing_table' ('house_structure_page_url') VALUES('test')' at line 1")During handling of the above exception, another exception occurred:Traceback (most recent call last):File "/Users/huangjing/downHouseInfo/MainF.py", line 238, in <module>cursor.execute("INSERT INTO %s (%s) VALUES(%s)",(table,key,value))File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/cursors.py", line 117, in executeself.errorhandler(self, exc, value)File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/connections.py", line 189, in defaulterrorhandlerraise errorclass(errorvalue)
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 ''huxing_table' ('house_structure_page_url') VALUES('test')' at line 1")
Exception ignored in: <bound method Cursor.__del__ of <pymysql.cursors.Cursor object at 0x10585ebe0>>
Traceback (most recent call last):File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/cursors.py", line 41, in __del__File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/cursors.py", line 47, in close
ReferenceError: weakly-referenced object no longer exists

但是,尝试执行

cursor.execute("INSERT INTO huxing_table (house_structure_page_url) VALUES(%s)",(value))

时,没有错误提示。

在错误提示第31行发现,执行的mysql语句中用%s替换的参数外加上了单引号。

''huxing_table' ('house_structure_page_url') VALUES('test')'

在mysql命令行终端进行测试,执行语句

mysql> insert into huxing_table (`house_structure_page_url`) values("test");
Query OK, 1 row affected (0.00 sec)

没有错误提示。而执行

mysql> insert into huxing_table ('house_structure_page_url') values("test");
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 ''house_structure_page_url') values("test")' at line 1

则有错误提示。再进行验证

mysql> insert into huxing_table (house_structure_page_url) values('test');
Query OK, 1 row affected (0.00 sec)

不出错。

mysql> insert into 'huxing_table' (house_structure_page_url) values("test");
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 ''huxing_table' (house_structure_page_url) values("test")' at line 1

出错,说明在mysql的insert语句中表名和列名外都不能加单引号,而值则可以加单引号。

就直接写语句好了。
最后的解决办法是插入一条数据写一条sql语句。

参考:https://www.jianshu.com/p/92026862a0e5
https://www.jianshu.com/p/855fdb50c26c

[288]关于MySQL的1064错误相关推荐

  1. mysql 1064_MYSQL #1064错误

    展开全部 出现这种情况是因为你建表的SQL语句有错误, 你的给出的代码里option为MYSQL关键字,不能直接写e5a48de588b662616964757a686964616f313334313 ...

  2. Mysql 1064错误

    已知mysql的报错信息为:1064, You have an error in your SQL syntax; check the manual that corresponds to your ...

  3. 1064mysql分区_如何解决mysql错误代码1064

    解决mysql错误代码1064的方法: mysql报1064错误是因为mysql语句的语法出错了,检查编写的mysql语句,表名和列名都不能加单引号,只有值可以加单引号,修改之后就可以了 示例如下:m ...

  4. mysql 错误代码1064_如何解决mysql错误代码1064

    解决mysql错误代码1064的方法: mysql报1064错误是因为mysql语句的语法出错了,检查编写的mysql语句,表名和列名都不能加单引号,只有值可以加单引号,修改之后就可以了 示例如下:m ...

  5. mysql创建数据库1064_Mysql创建表过程中报1064错误

    Mysql创建表过程中报1064错误 发布时间:2020-06-18 06:23:08 来源:51CTO 阅读:7528 作者:白羊IT 我在自己搭建的mysql服务中,在使用create table ...

  6. mysql 使用update 1064错误的原因和解决方法

    最近在更新mysql数据表时候,用sql语句处理两张表更新,语句如下: update table set name='dddd' from table  inner join dable  on  t ...

  7. 在navicat for mysql 创建函数,保存的时候出现1064错误

    在navicat for mysql 创建函数,保存的时候出现1064错误 解决方法:很有可能是mysql语句出现语法错误,仔细检查一下符号是否为英文,是否忘记写符号":"

  8. mysql1558错误,mysql删除用户错误ERROR 1558解决办法

    1.错误提示 ERROR 1558 (HY000): Column count of mysql.user is wrong. Expected 43, found 42. Created with ...

  9. php错误1064,求助,phpmyadmin导入sql文件提示1064错误

    求助,phpmyadmin导入sql文件提示1064错误 -- phpMyAdmin SQL Dump -- version 4.6.4 -- https://www. -- -- Host: 127 ...

最新文章

  1. Pytorch使用tensorboardX可视化。超详细
  2. MYSQL.版本查看-LINUX
  3. nginx安装与配置详解
  4. 2.11 矩阵和实数运算不同之处
  5. 解决在Python中使用Win32api报错的问题,No module named win32api
  6. linux华为路由器模拟器,华为路由器模拟器与实验内容.doc
  7. QTP自传之web常用对象
  8. mysql主从有关参数_mysql主从复制配置
  9. Qt qlabel 设置字体、大小、加粗等
  10. GBase数据库事务隔离级别
  11. 二进制与格雷码相互转换
  12. 【日常分享】RAM和ROM区别
  13. HTML制作个人名片
  14. 识破面试官的套路:十个典型的面试问题剖析
  15. CSS - 移动端布局(一)关键的前置知识
  16. 自动控制原理9.1---线性系统的状态空间描述(下)
  17. cron表达式每个月最后一天
  18. 红队武器库:fastjson小于1.2.68全漏洞RCE利用exp复现
  19. Python+Django实现简单HelloWord网页
  20. JSP邮编区号管理系统

热门文章

  1. 编译openwrt n2n遇到错误
  2. 七牛网CEO的架构师7种能力和学习线路图
  3. 同步等待 异步等待_异步/等待与承诺互操作性
  4. flutter之包管理
  5. 将两个iso镜像刻录到一张dvd光盘上,当然是做系统启动盘用
  6. KMZ转换为KML,KML转换为shp文件格式方法
  7. 51单片机之STC89C52RC最小系统板烧录说明
  8. 前端讲义64_AngularJS鼠标与键盘事件有关指令
  9. AI训练营金融风控学习笔记
  10. 抓包分析SSL/TLS连接建立过程