gpt4 book ai didi

python - 如何修复计数查询 mysql 中的输出(flask)

转载 作者:行者123 更新时间:2023-11-29 16:15:46 25 4
gpt4 key购买 nike

如何修复计数查询 mysql (flask) 中的输出

def addcart():
cur = mysql.connection.cursor()
if 'uid' not in session:
noOfItems = 0
else:
uid = session['uid']
cur.execute("SELECT count(id_phone) FROM cart WHERE id_users = " + str(uid))
noOfItems = cur.fetchall()
return (noOfItems)


@app.route('/')
def index():
noOfItems = addcart()
return render_template("index.html", noOfItems=noOfItems)

在我的导航栏中

<a href="#" class="btn btn-more i-left">
<small><span class="badge badge-primary">{{noOfItems}}</span></small>
</a>

OUTPUT : ({'count(id_phone)': 3},)

最佳答案

def get_num_of_items():
cur = mysql.connection.cursor()
if 'uid' not in session:
num_of_items= 0
else:
uid = session['uid']
cur.execute("SELECT count(id_phone) FROM cart WHERE id_users = " + str(uid))
# cur.fetchone() return a dict with a single key:value pair
num_of_items = [v for v in cur.fetchone().values()][0]
return num_of_items

关于python - 如何修复计数查询 mysql 中的输出(flask),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54807430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com