关于MySQL的1064错误
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错误的更多相关文章
- mysql 在windows下,使用 net start mysql 命令发生错误 服务名无效 或 1067
mysql 在windows下,使用 net start mysql 命令发生错误 :服务名无效 或 1067 先使用mysqld -install安装一下 删除data目录下的日志等文件(因为之前 ...
- service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误
service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误的方法如下: [root@ctohome.com ~]# service ...
- MySQL安装常见错误及解决方案
错误1:wizard安装最后一页,出现cannot create Windows service for mysql.error:0 错误 解决方法:打开命令行 输入 sc delete mysql ...
- MYSQL ERROR CODE 错误编号的意义
mysql error code(备忘) 转1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件 ...
- Mysql操作命令出现错误时消除/mysql数据导入txt
MySQL怎样取消错误命令 http://jingyan.baidu.com/album/546ae1851e9fd61149f28cef.html?picindex=6 mysql导入数据load ...
- mysql的1067错误 - 2
上一篇博文<mysql的1067错误>中由于日志配置问题产生1067错误. 由于要升级MySQL到V5.6,所以拷贝my.ini和数据文件到新的系统上. 在启动服务时,又出现1067错误! ...
- Linux 下一个 Mysql error 2002 错误解决
Linux 下一个 Mysql error 2002 错误解决 首先查看 /etc/rc.d/init.d/mysqld status 查看mysql它已开始. 假设启动的的话,先将数 ...
- 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 ...
- MySQL主从失败 错误Got fatal error 1236解决方法
--MySQL主从失败 错误Got fatal error 1236解决方法 ----------------------------------------------------2014/05/1 ...
随机推荐
- RGB颜色转换算法C语言实现
typedef unsigned short TUINT16; #define RGB565(R, G, B) \ (((TUINT16) ((R) >> 3)) << ...
- spring boot之入门Controller常用注解
Controller常用注解 @Controller 处理http请求 @RestController Spring4之后新加的注解,原来返回json数据需要@ResponseBody配合@Cont ...
- 远离压力,提高效率——Getting things done读书笔记
一.确定时间.空间和工具 二.收集阶段:填充工作篮 1. 这有助于你认识到自己面对的工用量. 2. 同时让你清楚"隧道的终点"在哪 ...
- 初识Java——循环语句
循环语句就是在一定条件下反复执行某一个操作.具体有三种方法实现: 1while循环语句 while语句也称作条件判断语句,它的循环方式为利用一个条件来控制是否要反复执行.语法如下: while(条件语 ...
- treeview调用数据库成树
目的:将数据库中的数据与树控件绑定背景:我们想在树控件中显示销售客户的层级列表,这个销售客户的分层是这样的,先按"大区",再按"省份",最后到"客户& ...
- apache压力测试工具的apache bench和JMeter的安装
Apache压力测试工具的安装 1,apache bench的安装 apache bench工具集成在http的软件包内,可以直接安装apache就可以. 当有些时候,我们不需要用到所有的软件包,我 ...
- OO,OO以后,及其极限
1.什么是软件开发? 软件开发的过程就是人们使用各种计算机语言将人们关心的现实世界映射到计算机世界的过程: 现在的计算机的数学理论基础是由计算机的开山鼻祖,大名鼎鼎的图灵于1937年提出的图灵机模型. ...
- 异步任务spring @Async注解源码解析
1.引子 开启异步任务使用方法: 1).方法上加@Async注解 2).启动类或者配置类上@EnableAsync 2.源码解析 虽然spring5已经出来了,但是我们还是使用的spring4,本文就 ...
- CSS基础知识01
一.CSS基础知识介绍 1.css是cascading style sheet层叠式样式表的简写 2.css小用法 加粗:font-weight:bold; 正常用:norma ...
- Flask入门之自定义过滤器(匹配器)
1. 动态路由的匹配器? 不知道这种叫啥名,啥用法,暂且叫做匹配器吧. Flask自带的匹配器可以说有四种吧(保守数字,就我学到的) 动态路由本身,可以传任何参数字符串或者数字,如:<user ...