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 execute
    result = self._query(query)
  File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/cursors.py", line 230, in _query
    conn.query(q)
  File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/connections.py", line 607, in query
    self._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_result
    result.read()
  File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/connections.py", line 869, in read
    self.first_packet = self.connection.read_packet()
  File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/connections.py", line 686, in read_packet
    packet.check_error()
  File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/connections.py", line 328, in check_error
    raise_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_exception
    raise 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 execute
    self.errorhandler(self, exc, value)
  File "/Users/huangjing/Library/Python/3.5/lib/python/site-packages/pymysql/connections.py", line 189, in defaulterrorhandler
    raise 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语句。

关于MySQL的1064错误的更多相关文章

  1. mysql 在windows下,使用 net start mysql 命令发生错误 服务名无效 或 1067

    mysql 在windows下,使用 net start mysql 命令发生错误 :服务名无效 或 1067  先使用mysqld -install安装一下 删除data目录下的日志等文件(因为之前 ...

  2. service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误

    service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误的方法如下: [root@ctohome.com ~]# service ...

  3. MySQL安装常见错误及解决方案

    错误1:wizard安装最后一页,出现cannot create Windows service for mysql.error:0 错误 解决方法:打开命令行 输入 sc delete mysql ...

  4. MYSQL ERROR CODE 错误编号的意义

    mysql error code(备忘) 转1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件 ...

  5. Mysql操作命令出现错误时消除/mysql数据导入txt

    MySQL怎样取消错误命令 http://jingyan.baidu.com/album/546ae1851e9fd61149f28cef.html?picindex=6 mysql导入数据load ...

  6. mysql的1067错误 - 2

    上一篇博文<mysql的1067错误>中由于日志配置问题产生1067错误. 由于要升级MySQL到V5.6,所以拷贝my.ini和数据文件到新的系统上. 在启动服务时,又出现1067错误! ...

  7. Linux 下一个 Mysql error 2002 错误解决

    Linux 下一个 Mysql error 2002 错误解决     首先查看 /etc/rc.d/init.d/mysqld status 查看mysql它已开始.     假设启动的的话,先将数 ...

  8. sql点滴37—mysql中的错误Data too long for column '' at row 1

    原文:sql点滴37-mysql中的错误Data too long for column '' at row 1   1.MYSQL服务 我的电脑——(右键)管理——服务与应用程序——服务——MYSQ ...

  9. MySQL主从失败 错误Got fatal error 1236解决方法

    --MySQL主从失败 错误Got fatal error 1236解决方法 ----------------------------------------------------2014/05/1 ...

随机推荐

  1. IOS动画(Core Animation)总结 (参考多方文章)

    一.简介 iOS 动画主要是指Core Animation框架.官方使用文档地址为:Core Animation Guide. Core Animation是IOS和OS X平台上负责图形渲染与动画的 ...

  2. FineReport性能调优的一些办法

    FineReport性能调优的基本思路,就要对应用服务器的内存大小进行合理的设置. 一般服务器默认的内存配置都比较小,在较大型的应用项目中,这点内存是不够的,因此需要加工使其调大. 各应用服务器的内存 ...

  3. UML类图的简单梳理

    依赖关系 Dependency Class Car{} Class Person{ int a; static int b public void buy(Car c){ int c; .... } ...

  4. 恶补web之二:css知识(3)

    css有3种定位机制:普通流,浮动和绝对定位. 除非专门指定,否则所有框都在普通流中定位,即普通流中的元素位置由元素在(x)html中的位置决定. 通过使用position属性,可以选择4种不同类型的 ...

  5. JQuery系统梳理

    JQuery在前端网页开发中可以说是非常常用了,它所拥有的强大功能足以让我们完成各式各样的效果. 一.JQuery基础语法 1. 使用JQuery必须先导入jquery.x.x.x.js文件: 2. ...

  6. Java—SSH(MVC)

    Java-SSH(MVC) JAVA三大框架的各自作用 hibernate是底层基于jdbc的orm(对象关系映射)持久化框架,即:表与类的映射,字段与属性的映射,记录与对象的映射 数据库模型 也就是 ...

  7. 微信小程序中自定义函数的学习使用

    新手,最近在给学校搞个党费计算器.需要自己定义函数来实现某个功能. 1.无参函数: 函数都是写在js文件里面的. Page({ data:{ income1:'0', }, cal:function( ...

  8. es6(五):class关键字(extends,super,static)

    ES5中,生成对象通过构造函数: function A(name,age){ this.name=name; this.age=age } // 在A的prototype属性上定义一个test方法,即 ...

  9. 解决ExtJs Uncaught TypeError: c is not a constructor错误

    ExtJs项目使用sencha app build编译以后,浏览时很容易抛出Uncaught TypeError: c is not a constructor的错误,而且会加载没有名称的js,例如 ...

  10. 浏览器选择最新IE渲染

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />