我正在做CS0问题集7 - CS50财务,他们使用flask和SQLite。
当我尝试在phpliteadmin上访问stock_name =“s”时,我得到相应的行,但是当我尝试通过代码访问时,我得到空数组[],而它应该输出[{key:value,etc ... }],请参阅注释#HERE。
它有什么问题?如何访问我的数据库。
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
"""Sell shares of stock."""
if request.method=="POST":
if not request.form.get("symbol"):
return apology("Symbol name cant be empty")
elif request.form.get("symbol"):
rows=db.execute('SELECT * FROM portfolios WHERE username=:username AND stock_name=:stock_name',username=session["user_id"],stock_name="s")
print(rows)#**HERE**
if rows==[]:
return apology("No such stock name")
elif not request.form.get("shares"):
return apology("Shares missing")
elif request.form.get("shares"):
try:
number_shares=int(float(request.form.get("shares")))
except:
return apology("Invalid shares")
elif rows[0]["shares"]<request.form.get("shares"):
return apology("Sorry not enough shares")
rows=db.execute('SELECT * FROM portfolios WHERE username=:username AND stock_name=:stock_name',username="u",stock_name="s")
print(rows)
return render_template("sell.html")
答案 0 :(得分:0)
所以我发现它与python中的字符串连接有关,因此我使用+符号连接,如: **&#34; SELECT * FROM portfolio WHERE username =&#34; + usernameValue +&#34; \&#34;&#34;