【问题标题】:PyMySQL interface error keeps occurring in codePyMySQL接口错误在代码中不断发生
【发布时间】:2019-11-30 13:49:03
【问题描述】:

在执行代码期间,我不断收到 PyMySQL 接口错误。

我尝试过为每次执行创建一个游标并关闭它,但它不起作用。

        try:
            cursor = connection.cursor()
            sql = "SELECT `user_id` FROM `voters` WHERE `user_id`= %s"
            cursor.execute(sql, (user,))
            result = cursor.fetchone()
            cursor.close()

            if result == None:
                cursor = connection.cursor()
                sql = "INSERT INTO `voters` (`user_id`) VALUES (%s)"
                cursor.execute(sql, (user,))
                cursor.close()

            cursor = connection.cursor()

            sql = "UPDATE `voters` SET `vote_cooldown` = %s WHERE `user_id`= %s"
            cursor.execute(sql, (time, user))
            cursor.close()

        finally:
            pass

完整回溯:

[2019-07-22 16:40:33,960] ERROR in app: Exception on /webhook [POST]         
Traceback (most recent call last):                                              
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2446, in wsgi
_app                                                                            
    response = self.full_dispatch_request()                                     
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1951, in full
_dispatch_request                                                               
    rv = self.handle_user_exception(e)                                          
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1820, in hand
le_user_exception                                                               
    reraise(exc_type, exc_value, tb)                                            
  File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in re
raise                                                         
    raise value                                                                 
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1949, in full
_dispatch_request                                                               
    rv = self.dispatch_request()                                                
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1935, in disp
atch_request                                                                    
    return self.view_functions[rule.endpoint](**req.view_args)                  
  File "webhook.py", line 38, in webhook                                        
    cursor.execute(sql, (user,))
  File "/usr/local/lib/python3.6/dist-packages/pymysql/cursors.py", line 170, i$
 execute
    result = self._query(query)
  File "/usr/local/lib/python3.6/dist-packages/pymysql/cursors.py", line 328, i$
 _query
    conn.query(q)
  File "/usr/local/lib/python3.6/dist-packages/pymysql/connections.py", line 51$
, in query
    self._execute_command(COMMAND.COM_QUERY, sql)
  File "/usr/local/lib/python3.6/dist-packages/pymysql/connections.py", line 74$
, in _execute_command
    raise err.InterfaceError("(0, '')")
pymysql.err.InterfaceError: (0, '')

预期结果:数据库正常更新

实际结果:

pymysql.err.InterfaceError: (0, '')

【问题讨论】:

  • 你为什么要在获取结果之前关闭游标?
  • 我刚刚添加了这个,还没有测试过。在此之前,我在关闭游标之前获取结果。无法弄清楚如何摆脱错误。
  • 将帖子编辑为原始代码。
  • 你能发布完整的回溯错误吗?这样我们就可以更好地了解错误的来源。
  • 编辑了带有完整回溯的帖子。

标签: mysql python-3.x pymysql


【解决方案1】:

来自this answer,很明显错误是因为您正在关闭游标并为每个操作生成它。尝试使用单个光标运行代码并检查结果。

除此之外,我注意到insert 查询不是commited,这是使您的查询执行提交到表中所必需的。

我建议对所有这些类型的查询操作使用一个函数,如下面的代码。

def query_action(connection, query, action):
    cursor = connection.cursor()
    cursor.execute(query)
    if action == 'insert' or action == 'update':
        cursor.commit()
        return None
    if action == 'selectone':
        return cursor.fetchone()
    if action == 'select':
        return cursor.fetchall()

def db_actions(connection):
    try:
        sql = "SELECT `user_id` FROM `voters` WHERE `user_id`= %s"
        result = query_action(connection, (sql,(user)), 'selectone')

        if result == None:
            sql = "INSERT INTO `voters` (`user_id`) VALUES (%s)"
            query_action(connection, (sql,(user)), 'insert')

        sql = "UPDATE `voters` SET `vote_cooldown` = %s WHERE `user_id`= %s"
        query_action(connection, (sql, (time, user)), 'select')

    finally:
        pass

if __name__ == '__main__':
    connection = pymysql.connect(....)
    db_actions(connection)

代码中存在错误的可能性。在完全执行之前进行测试运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 2021-11-07
    相关资源
    最近更新 更多