sqlite3“OperationalError:near”)“:语法错误”python

时间:2017-04-14 15:46:25

标签: python python-3.x sqlite

此功能应检查用户在提示后输入的成分。当我在sqlite中尝试这个查询语句时(当然没有用户输入)它可以工作。在python中它说有错误!

def checkIngredient():

   usrIngredient = input("\nEnter an ingredient that you would like to make a drink with: \n\n")

    query = c.execute("SELECT DRDESC FROM Drinks WHERE DRDRID IN "
    "(SELECT DTDRID FROM Detail WHERE INGID ="
    "(SELECT INGID FROM Ingredients WHERE INDESC LIKE))", (usrIngredient,))


    resultset = c.fetchall()

    for result in resultset:
        if resultset is not None:
            print(result)
        else:
            print("Sorry, there are no drinks with that ingredient")

2 个答案:

答案 0 :(得分:1)

您的查询中未包含 SQL参数占位符。您必须在查询中为要插入的每个值放置query = c.execute(""" SELECT dr.DRDESC FROM Drinks as dr INNER JOIN Detail as dtd on dr.DRDRID == dtd.DTDRID INNER JOIN Ingredients as ing on dtd.INGID = ing.INGID WHERE ing.INDESC LIKE ? """, (usrIngredient,))

usrIngredient

您可能想要考虑进行连接,我怀疑使用嵌套选择将会很好地执行。我怀疑以下内容会更好:

LIKE

如果您希望将%视为子字符串搜索,则必须向其添加通配符才能使usrIngredient = '%{}%'.format(usrIngredient) 正常工作。使用eggs个字符围绕值:

%eggs%

这会将字符串eggs替换为 <button type="reset">Reset</button> ,以查找包含文本<form> <input type="checkbox" name="check" value="yes" /> Option 1<br /> <button type="reset">Reset</button> </form>的所有匹配成分说明。

答案 1 :(得分:0)

也许是这样的:

query=c.execute("""SELECT Drinks.DRDESC
                   FROM Drinks JOIN Detail JOIN Ingredients
                   ON Drinks.DRDRID = Detail.DTDRID
                   AND Detail.INGID = Ingredients.INGID
                   where Ingredients.INDESC like ?""",(usrIngredient,) )