You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python mysql.connector.fetchall()返回格式不一致且存在冗余数据问题

问题根因
    1. 函数变量作用域与初始化问题:你定义的res变量仅在else分支(数据库连接成功分支)中赋值,一旦数据库连接失败进入except分支,res没有局部初始化,函数会直接读取全局作用域下残留的res值(也就是之前某次调用成功时的返回结果),这是结果随机、出现冗余旧数据的核心原因。
    1. 资源释放逻辑位置错误:cursor.close()和myDatabase.close()的缩进层级错误,和try块平级,没有放在finally分支中。一旦连接过程抛出异常,myDatabase和cursor实例未初始化,直接调用close()会抛出额外错误,进一步打断正常的res赋值逻辑。
    1. Flask多线程特性影响:Flask默认以多线程模式处理请求,如果res被定义为全局变量,多个请求并发调用exeQuery时会互相篡改res的值,也会导致返回结果混乱。
    1. API使用不符合预期:fetchall()的返回值固定为列表类型,不管匹配到多少行结果都会用列表包裹。你看到的单独元组返回结果,本质也是某次异常分支返回的旧的fetchone()结果或者其他逻辑的残留值。
修复方案

首先修改通用查询方法exeQuery,解决作用域、资源释放问题,同时适配单条查询的需求:

import mysql.connector
# 假设dbLoginInfo是全局可访问的数据库只读配置
def exeQuery(query, data=None, db_edit=False, fetch_single=False):
    res = None # 函数开头初始化局部res,避免读取全局残留值
    my_database = None
    cursor = None
    try:
        my_database = mysql.connector.connect(**dbLoginInfo)
        cursor = my_database.cursor()
        if db_edit:
            cursor.execute(query, data) if data else cursor.execute(query)
            my_database.commit()
            res = cursor.rowcount # 写操作返回影响行数,符合常规使用逻辑
        else:
            cursor.execute(query, data) if data else cursor.execute(query)
            # 根据参数决定取单条还是全部结果
            res = cursor.fetchone() if fetch_single else cursor.fetchall()
    except mysql.connector.Error as e:
        print('[ERROR WHILE OPERATING DATABASE]: ', e)
    finally:
        # 不管是否异常都释放资源,调用前先判空避免报错
        if cursor:
            cursor.close()
        if my_database:
            my_database.close()
    return res

然后修改Flask接口中的调用逻辑,指定fetch_single=True来获取单条结果,符合你的预期:

@app.route("/profil/delete", methods= ['POST'])
@token_required
def deleteProfil():
    dicUser = decodeToken(request.args.get('token'))
    profilName = request.args.get('profilName')
    # 新增fetch_single参数,直接获取单条元组结果
    path = exeQuery(
        'SELECT profilbild FROM Profil WHERE profilName = %s AND konto_email = %s', 
        (profilName, dicUser['user']), 
        db_edit=False,
        fetch_single=True
    )
    print(path) # 匹配到结果时输出为 ('pics/jj@gmail.de/DelProf.png',),无匹配为None
    return Response(status = 200)

内容的提问来源于stack exchange,提问作者Pixelherz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 10:06:03