V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
LightingX
V2EX  ›  问与答

Python flask+pymysql 数据库访问异常一次之后就全是异常

  •  
  •   LightingX · 2019-01-02 21:17:07 +08:00 · 1437 次点击
    这是一个创建于 2136 天前的主题,其中的信息可能已经有所发展或是发生改变。

    跑着跑着莫名其妙,某次请求发出之后就异常了。本来好好的,但是之后就全是这样的异常。

    [2019-01-02 21:02:16,374] ERROR in app: Exception on /users [GET]
    Traceback (most recent call last):
      File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 2292, in wsgi_app
        response = self.full_dispatch_request()
      File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1815, in full_dispatch_request
        rv = self.handle_user_exception(e)
      File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1718, in handle_user_exception
        reraise(exc_type, exc_value, tb)
      File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 35, in reraise
        raise value
      File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1813, in full_dispatch_request
        rv = self.dispatch_request()
      File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1799, in dispatch_request
        return self.view_functions[rule.endpoint](**req.view_args)
      File "controller.py", line 31, in get_users
        user_num = dto.count_user()
      File "/root/spider/web/dto.py", line 34, in count_user
        cursor.execute("SELECT COUNT(*) FROM user")
      File "/usr/local/lib/python3.5/dist-packages/pymysql/cursors.py", line 170, in execute
        result = self._query(query)
      File "/usr/local/lib/python3.5/dist-packages/pymysql/cursors.py", line 328, in _query
        conn.query(q)
      File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 516, in query
        self._execute_command(COMMAND.COM_QUERY, sql)
      File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 750, in _execute_command
        raise err.InterfaceError("(0, '')")
    pymysql.err.InterfaceError: (0, '')
    

    不懂是什么情况。

    代码如下: 接口:

    @app.route('/user/<uid>')
    def get_user_photo(uid):
        uid = int(uid)
        offset = request.args.get('offset', type=int, default=0)
        size = request.args.get('size', type=int, default=8)
        photo_num = dao.count_photo(uid)
        page_info = {}
        if offset+size > photo_num:
            size = photo_num - offset
            page_info = {'has_next': False}
        else:
            page_info = {'has_next': True, 'next': offset+size}
        result_data = dao.get_user_photo_info(uid, offset, size)
        for photo in result_data:
            photo['date'] = str(photo['date'])
        result_data = dict(page_data=result_data)
        result_data['page_info'] = page_info
        result_data['count'] = photo_num
        result_data['user_info'] = dao.get_user_info(uid)
        return filtrate_headers(json.dumps(BaseResponse(200, result_data), default=lambda obj: obj.__dict__))
    

    dao:

    def count_user():
        cursor.execute("SELECT COUNT(*) FROM user")
        result = cursor.fetchall()
        return result[0]['COUNT(*)']
     
    
    def get_user_info(uid):
        get_user_sql = "SELECT * FROM user WHERE uid=%d" %uid
        cursor.execute(get_user_sql)
        result = cursor.fetchall()
        if len(result) == 1:
            return result[0]
        else:
            return dict()
    
    1 条回复    2019-01-03 17:25:36 +08:00
    julyclyde
        1
    julyclyde  
       2019-01-03 17:25:36 +08:00
    看数据库那边的日志
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4225 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 04:08 · PVG 12:08 · LAX 20:08 · JFK 23:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.