600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 使用pymysql报错:pymysql.err.InternalError: Packet sequence number wrong - got 5 expected 1

使用pymysql报错:pymysql.err.InternalError: Packet sequence number wrong - got 5 expected 1

时间:2022-09-13 09:54:39

相关推荐

使用pymysql报错:pymysql.err.InternalError: Packet sequence number wrong - got 5 expected 1

环境:Python 3.8.5

在使用pymysql去数据库查询数据时,出现了如下错误:

Exception ignored in: <function MysqlPoolClient.__del__ at 0x000001107810DC10>Traceback (most recent call last):File "E:\test\work\myspider\utils\mysql_pool.py", line 39, in __del__self.dispose()File "E:\test\work\myspider\utils\mysql_pool.py", line 206, in disposeself.end('commit')File "E:\test\work\myspider\utils\mysql_pool.py", line 196, in mit()File "E:\test\venv1\lib\site-packages\dbutils\steady_db.py", line 435, in commitraise error # re-raise the original errorFile "E:\test\venv1\lib\site-packages\dbutils\steady_db.py", line 426, in mit()File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\pymysql\connections.py", line 422, in commitself._read_ok_packet()File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\pymysql\connections.py", line 396, in _read_ok_packetpkt = self._read_packet()File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\pymysql\connections.py", line 658, in _read_packetraise err.InternalError(pymysql.err.InternalError: Packet sequence number wrong - got 5 expected 1

最开始查找的相关资料,都是说多线程未加锁导致类似问题的出现,例如:python pymysql 多线程 读写数据库 报错 Packet sequence number wrong

但实际上,mysql_pool.py是基于pymysql封装的模块,已经注意了多线程锁的问题,所以考虑实际问题不是因为未加锁。

重新使用traceback模块打印异常,发现了如下错误:

Traceback (most recent call last):File "E:/test/work/myspider/spider_files/tianyancha_new/mysql_to_es.py", line 52, in get_one_from_companyresult = client.getOne(sql)File "E:\test\work\myspider\utils\mysql_pool.py", line 88, in getOnecount = self._cursor.execute(sql)File "E:\test\venv1\lib\site-packages\dbutils\steady_db.py", line 598, in tough_methodresult = method(*args, **kwargs) # try to executeFile "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\pymysql\cursors.py", line 163, in executeresult = self._query(query)File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\pymysql\cursors.py", line 321, in _queryconn.query(q)File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\pymysql\connections.py", line 505, in queryself._affected_rows = self._read_query_result(unbuffered=unbuffered)File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\pymysql\connections.py", line 724, in _read_query_resultresult.read()File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\pymysql\connections.py", line 1076, in readself._read_result_packet(first_packet)File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\pymysql\connections.py", line 1146, in _read_result_packetself._read_rowdata_packet()File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\pymysql\connections.py", line 1184, in _read_rowdata_packetrows.append(self._read_row_from_packet(packet))File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\pymysql\connections.py", line 1203, in _read_row_from_packetdata = converter(data)TypeError: conversion from bytes to Decimal is not supported

看见“Decimal”想起来,要查询的数据中,有个字段是Decimal类型,然后继续查找相关资料:

pymysql TypeError: conversion from bytes to Decimal is not supported,这位大佬直接去把pymysql源码做了修改,应该也能解决问题(未实验)

后来参考了这个:python-Django錯誤:不支持從字節到十進位的轉換,大体意思是,由于use_unicode设置为False导致。

use_unicode是连接mysql时设置的一个参数,在python3中,该参数默认是True,但是在封装的mysql_pool.py中,use_unicode设置的为False,所以产生了上面的错误。重新将use_unicode改为True,即可避免上述问题的出现了。

参考:PyMySQL 官方文档

另外,当use_unicode设置为False时,查询结果里的文本对象都是字节类型;设置为True后,获取到的文本对象是字符串类型的数据。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。