1. 程式人生 > >pymysql.err.InterfaceError Interface Error: (0, '')解決辦法

pymysql.err.InterfaceError Interface Error: (0, '')解決辦法

時間一長,什麼樣的坑都能出現。

Flask的專案碰到了這樣的錯誤:

pymysql.err.InterfaceError

InterfaceError: (0, '')

Traceback (most recent call last)

  • File "/usr/lib64/python2.7/site-packages/flask/app.py", line 2309, in __call__

    return self.wsgi_app(environ, start_response)

  • File "/usr/lib64/python2.7/site-packages/flask/app.py", line 2295
    , in wsgi_app

    response = self.handle_exception(e)

  • File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1741, in handle_exception

    reraise(exc_type, exc_value, tb)

  • File "/usr/lib64/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app

    response = self.full_dispatch_request()

  • File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1815
    , in full_dispatch_request

    rv = self.handle_user_exception(e)

  • File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception

    reraise(exc_type, exc_value, tb)

  • File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request

    rv = self.dispatch_request()

  • File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request

    return self.view_functions[rule.endpoint](**req.view_args)

File "/home/user1/exihibition/exihibition.py", line 22, in twitter

cur.execute(sql)

  • File "/usr/lib64/python2.7/site-packages/pymysql/cursors.py", line 170, in execute

    result = self._query(query)

  • File "/usr/lib64/python2.7/site-packages/pymysql/cursors.py", line 328, in _query

    conn.query(q)

  • File "/usr/lib64/python2.7/site-packages/pymysql/connections.py", line 515, in query

    self._execute_command(COMMAND.COM_QUERY, sql)

  • File "/usr/lib64/python2.7/site-packages/pymysql/connections.py", line 745, in _execute_command

    raise err.InterfaceError("(0, '')")

InterfaceError: (0, '')

在網上看到的原因是說MySQL連線超過8小時沒有任何操作時,MySQL會自動斷開連線。一種推薦的方式是使用db.ping(),即在每次進行查詢或者插入相關操作時,先檢查資料庫的連線是否正常,如果斷開了,則重新連線。

db.ping(reconnect=True)
cur.execute(sql)
db.commit()