【问题标题】:Why I get the wrong output format of counting with flask?为什么我得到错误的烧瓶计数输出格式?
【发布时间】:2021-10-19 05:41:19
【问题描述】:

我现在正在尝试计算 phpMyAdmin 数据库中的数据数量。为什么我会得到像 (2,) 这样的输出,而不仅仅是像 2 这样的数字格式?

app.py 代码

@app.route("/adminPanel", methods=['GET', 'POST'])
def adminPanel():
    if 'adminName' in session:
    cur = mysql.connection.cursor()
    result1 = cur.execute("SELECT COUNT(*) FROM adminaccount")
    display2 = cur.fetchone()
    cur.close()

    adminName = session['adminName']
    return render_template("adminPanel.html", adminName=adminName, result1=result1, display2=display2)

else:
    return redirect(url_for('adminLogin'))

结果 result of the output

【问题讨论】:

    标签: mysql database flask output


    【解决方案1】:

    Select 返回结果,每个结果都是一个值的元组。您的选择只要求一个值,因此您将得到一个只有一个值的元组。所以:

    display2     # this is the whole row
    display2[0]  # this is the value of the first "column"
    

    因此,在处理每行 1 列和每行更多列时,您具有相同的界面。

    【讨论】:

      猜你喜欢
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 2019-01-25
      • 2020-12-04
      • 2013-08-17
      • 1970-01-01
      • 2021-11-26
      相关资源
      最近更新 更多