相关疑难解决方法(0)

查询期间与MySQL服务器的连接丢失

我有一个巨大的表,我需要处理其中的所有行.我总是得到这个丢失的连接消息,我无法重新连接并将光标恢复到它的最后位置.这基本上就是我在这里的代码:

#
import MySQLdb

class 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)
#
Run Code Online (Sandbox Code Playgroud)

但我总是得到这个:

#
Traceback (most recent call last):
  File "teste.py", line 124, in <module>
   run()
 File "teste.py", line 109, in run
   for row in data: …
Run Code Online (Sandbox Code Playgroud)

python mysql

53
推荐指数
6
解决办法
11万
查看次数

标签 统计

mysql ×1

python ×1