简体   繁体   English

sqlite3“ OperationalError:附近”)“:语法错误” python

[英]sqlite3 “OperationalError: near ”)“: syntax error” python

This function is supposed to check for the ingredient the user entered following the prompt. 该功能应该检查用户在提示后输入的成分。 When I try this query statement in sqlite (without the user input of course) it works. 当我在sqlite中尝试此查询语句时(当然,无需用户输入),它会起作用。 In python it says there is an error! 在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")

You haven't included a SQL parameter placeholder in your query. 您的查询中未包含SQL参数占位符 You'll have to place a ? 您必须放置一个? in the query for each value you want to interpolate: 在查询中要插入的每个值:

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

You may want to look into making joins, I doubt that using nested selects is going to perform all that well. 您可能要研究进行联接,我怀疑使用嵌套选择是否会很好地执行。 I suspect that the following would work better: 我怀疑以下方法会更好:

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,))

If you wanted usrIngredient to be treated as a substring search , you'll have to add wildcards to it for LIKE to work correctly. 如果usrIngredient视为子字符串搜索 ,则必须向其中添加通配符,以使LIKE正常工作。 Surround the value with % characters: %字符括起来的值:

usrIngredient = '%{}%'.format(usrIngredient)

This replaces the string eggs with %eggs% to find all matching ingredient descriptions that contain the text eggs . 这将用%eggs%替换字符串eggs ,以查找包含文本eggs所有匹配成分描述。

Maybe something like this: 也许是这样的:

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,) )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sqlite3“OperationalError:near”(“:语法错误”python - sqlite3 “OperationalError: near ”(“: syntax error” python sqlite3.OperationalError:靠近“WHERE”:语法错误(Python 2,sqlite3) - sqlite3.OperationalError: near “WHERE”: syntax error (Python 2, sqlite3) Python-sqlite3 sqlite3.OperationalError:接近“%”:语法错误? - Python - sqlite3 sqlite3.OperationalError: near “%”: syntax error? SQLite3 OperationalError:靠近“(”:语法错误 - SQLite3 OperationalError: near “(”: syntax error Sqlite3:OperationalError:在“ TABLE”附近:语法错误 - Sqlite3: OperationalError: near “TABLE”: syntax error Python和sqlite3抛出错误:sqlite3.OperationalError:near“s”:语法错误 - Python and sqlite3 throwing an error: sqlite3.OperationalError: near “s”: syntax error Python2.7-SQLite3库输出错误消息“ sqlite3.OperationalError:靠近“?”:语法错误” - Python2.7 - SQLite3 library outputs error message “sqlite3.OperationalError: near ”?“: syntax error” Python SQlite3更新函数sqlite3.OperationalError:“ WHERE”附近:语法错误 - Python SQlite3 Update function, sqlite3.OperationalError: near “WHERE”: syntax error Python:sqlite3.OperationalError:在“ &lt;”附近:语法错误(使用HTML源代码更新sqlite3字段) - Python: sqlite3.OperationalError: near “<”: syntax error (updating sqlite3 field with html source code) sqlite3.OperationalError:“,”附近:语法错误python - sqlite3.OperationalError: near “,”: syntax error python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM