查询期间与MySQL服务器的连接丢失我有一个巨大的表,我需要处理其中的所有行。我总是得到这个丢失的连接消息,我无法重新连接并将光标恢复到它的最后位置。这基本上就是我在这里的代码:#import MySQLdbclass DB:
conn = None
def connect(self):
self.conn = MySQLdb.connect('hostname', 'user', '*****', 'some_table', cursorclass=MySQLdb.cursors.SSCursor)
def query(self, sql):
try:
cursor = self.conn.cursor()
cursor.execute(sql)
except (AttributeError, MySQLdb.OperationalError):
self.connect()
cursor = self.conn.cursor()
cursor.execute(sql)
return cursor##db = DB()sql = "SELECT bla FROM foo"data = db.query(sql)for row in data:
do_something(row)#但我总是得到这个:#Traceback (most recent call last):
File "teste.py", line 124, in <module>
run()
File "teste.py", line 109, in run for row in data:
File "/usr/lib64/python2.5/site-packages/MySQLdb/cursors.py", line 417, in next
row = self.fetchone()
File "/usr/lib64/python2.5/site-packages/MySQLdb/cursors.py", line 388, in fetchone
r = self._fetch_row(1)
File "/usr/lib64/python2.5/site-packages/MySQLdb/cursors.py", line 285, in _fetch_row return self._result.fetch_row(size, self._fetch_type)
_mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')
Exception _mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query') in <bound method SSCursor.__del__ of <MySQLdb.cursors.SSCursor object at 0x7f7e3c8da410>> ignored#你有什么主意吗?
3 回答
HUWWW
TA贡献1874条经验 获得超12个赞
扩展mysql服务器的max_allowed_packet有三种方法:
更改mysql服务器计算机上的
max_allowed_packet=64M
文件/etc/mysql/my.cnf
并重新启动服务器在mysql服务器上执行sql:
set global max_allowed_packet=67108864;
Python连接到mysql后执行sql:
connection.execute('set max_allowed_packet = 67108864')
添加回答
举报
0/150
提交
取消