亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

查詢期間與MySQL服務器的連接丟失

查詢期間與MySQL服務器的連接丟失

慕姐4208626 2019-08-08 16:14:02
查詢期間與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有三種方法:

  1. 更改mysql服務器計算機上的max_allowed_packet=64M文件/etc/mysql/my.cnf并重新啟動服務器

  2. 在mysql服務器上執行sql: set global max_allowed_packet=67108864;

  3. Python連接到mysql后執行sql:

connection.execute('set max_allowed_packet = 67108864')


查看完整回答
反對 回復 2019-08-08
?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

確保在連接前關閉光標。我用這個解決了我的問題:

if cur and con:                        
    cur.close() 
    con.close()


查看完整回答
反對 回復 2019-08-08
  • 3 回答
  • 0 關注
  • 1036 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號