我在标题中得到了TypeError,但不太清楚为什么。在DB-API’s parameter substitution之后,这应该可以正常工作。
这是我正在处理的转化网站。我使用基本单位乘数将一个单位转换为另一个单位。所以例如厘米到码就像这样。
amount =要转换的用户输入
row_1 =用户选择的第一个基本单位乘数(cm到米)
row_2 =用户选择的第二个基准乘数(米到码)
conversion_results = amount * row_1 * row_2 - 1
我试过'%s'但是明白这会让我处于危险之中,所以下面就是我目前所坚持的情况。我正在使用Python 3和phpLiteAdmin来处理SQL表。
编辑我试图简单地输入一个字符串,例如(Meter,),(“Meter”,),(Meter)和(“Meter”),我仍然得到了同样的错误。我添加了我的SQL表的图片,因为它可能有问题吗?
from cs50 import SQL
from flask import Flask, flash, redirect, render_template, request, session,
url_for
from flask_session import Session
from passlib.apps import custom_app_context as pwd_context
from tempfile import gettempdir
from helpers import *
db = SQL("sqlite:///countertapp.db")
@app.route("/length", methods=["GET", "POST"])
def length():
if request.method == "GET":
return render_template("length.html")
if request.method == "POST":
amount = request.form.get("amount")
if not amount:
return apology("Enter some numbers")
elif amount.isalpha():
return apology("No letters please")
elif any(c.isalpha() for c in amount):
return apology("I said no letters!")
convert_from = request.form.get("unit_1")
convert_to = request.form.get("unit_2")
row_1 = db.execute("SELECT bum FROM conversion WHERE unit = ?", (convert_from,))
row_2 = db.execute("SELECT bum FROM conversion WHERE unit = ?", (convert_to,))
conversion_result = int(amount) * row_1[0]["bum"] * row_2[0]["bum"]**-1
return render_template("length_converted.html", result = conversion_result )
答案 0 :(得分:0)
原来我不得不给我们分号。以下是有效的。
row_1 = db.execute("SELECT bum FROM conversion WHERE unit = :id", id =
request.form.get("unit_1"))
row_2 = db.execute("SELECT bum FROM conversion WHERE unit = :id", id =
request.form.get("unit_2"))