关于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语句。

原文地址:https://www.cnblogs.com/zhouxinfei/p/9038512.html

时间: 2024-11-06 01:22:24

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

Translating SQLException with SQL state &#39;42000&#39;, error code &#39;1064&#39;错误

PostgreSQL, Sybase]2018-04-12 14:18:55 [org.springframework.jdbc.support.SQLErrorCodesFactory]-[DEBUG] Looking up default SQLErrorCodes for DataSource [{ CreateTime:"2018-04-12 14:18:43", ActiveCount:1, PoolingCount:0, CreateCount:1, DestroyCoun

mysql 数据库表错误 修复 总结

mysql 数据库坏表修复 萝卜白菜,各有所爱,能干活.能修复表才是王道!!! 修复之前谨记:先备份数据库 (备份完成后再进行以下修复操作) 可以mysqldump -A  > all.sql   进行全库备份  (mysqldump导出错误的时候可以省略错误的表进行导出其他的数据添加选项   --ignore-table=table_name  )  也可以进入到/usr/local/shell/ 执行  mysql_backup.sh进行备份数据库  以上两种方式都不可以备份 可以进入到/d

如何快速解决MySQL 1032 主从错误

3分钟解决MySQL 1032主从错误 Part1:写在最前1032错误----现在生产库中好多数据,在从库误删了,生产库更新后找不到了,现在主从不同步了,再跳过错误也没用,因为没这条,再更新还会报错 解决方案 Part1:临时方案 mysql> stop slave;Query OK, 0 rowsaffected (0.00 sec) mysql> set global sql_slave_skip_counter=1;Query OK, 0 rowsaffected (0.00 sec)

在查MySQL 1366的错误原因

在查MySQL 1366的错误原因时,发现一些有趣的回答. 不过,还是有人给出了比较好的解决方法 MySQL 1366错误大致描述如下 1. SQL Error: 1366: Incorrect string value: "\xE8\xAF\xA6\xE7\xBB\x86-" for column "address" at row 1 解决办法:检查数据库此字段的字符集与整理字符集是否与SQL语句传递数据的字符集相同:不相同则会引发MySQL1366错误. 修改M

Mysql添加用户错误:ERROR 1364 (HY000): Field &#39;ssl_cipher&#39; doesn&#39;t have a default value解决方法

添加用户 insert into mysql.user(Host,User,Password) values("localhost","test",password("1234")); 报以下的错误 ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value错误 mysql5.1以上版本,我是在5.6版本上操作的. 错语原因: mysql用户表的中某些字段不能为空,没有默

mysql 主从复制常见错误

Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always

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

mysql 在windows下,使用 net start mysql 命令发生错误 :服务名无效 或 1067 先使用mysqld -install安装一下 删除data目录下的日志等文件(因为之前的错误或pid文件有问题) net start mysql 启动 来自为知笔记(Wiz)

启动mysql出现1067错误

0. 打开mysql\bin\my.ini,查找[mysqld],在[mysqld]下面添加一行文字,skip-grant-tables 即组成 [mysqld] skip-grant-tables[本人经过此方法一次性成功] 方法一:1.打开my.ini文件,找到default-storage-engine=InnoDB这一行,把它改成default-storage-engine=MyISAM. 2.删除在MySQL安装目录下的Data目录中的ib_logfile0和ib_logfile1 3

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

错误1:wizard安装最后一页,出现cannot create Windows service for mysql.error:0 错误 解决方法:打开命令行 输入 sc delete mysql 命令即    C:>sc delete mysql    [SC] DeleteService SUCCESS 重起电脑,或者卸载后重启再安装,这样就好了.我是卸载后重启的. 卸载重新安装要注意一个问题,在控制面板中卸载后,要去c:\Documents and Settings\All Users\