简体   繁体   English

搜索关键字,由用户使用 sqlite3 和 python 3.7 输入

[英]Searching for a keyword, inputted by the user using sqlite3 and python 3.7

I want to search for a post using the keyword inputted from the user in python 3.7, I came up with two solutions but none worked, here's what I did.我想使用用户在 python 3.7 中输入的关键字搜索帖子,我想出了两个解决方案,但都没有奏效,这就是我所做的。

Possible solution 1:可能的解决方案1:

c.execute("SELECT pid FROM posts WHERE title LIKE '%?%';", (keyWord,))

However, it gives me this error但是,它给了我这个错误

Exception has occurred: ProgrammingError
Incorrect number of bindings supplied. The current statement uses 0, and there are 1 supplied.

Possible solution 2:可能的解决方案2:

keyWord = "'%" + keyWord + "%'"
c.execute("SELECT pid FROM posts WHERE title LIKE ?;", (keyWord,))

This time returns an empty list, I tried hardcoding a value using the normal method:这次返回一个空列表,我尝试使用普通方法对值进行硬编码:

c.execute("SELECT pid FROM posts WHERE title LIKE '%a%';")

and it did return the desired values, so the code should output a result.它确实返回了所需的值,因此代码应该输出结果。

Try:尝试:

c.execute("SELECT pid FROM posts WHERE title LIKE ?", ('%'+keyWord+'%',))

In your case, you can also use instr which return the index of the substring you're searching for (0 otherwise):在您的情况下,您还可以使用instr返回您正在搜索的子字符串的索引(否则为 0):

c.execute("SELECT pid FROM posts WHERE instr(title, ?) > 0", (keyWord,))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM