在 python 中传递参数的 postgres 问题



我有这段代码,它取标题或ISBN,或一本书的作者,并从数据库中检索所有匹配的数据。

问题在于传递参数行,它仅检索有关用户输入的数据的第一条记录。

我尝试在数据库控制台中使用 select 语句,它检索了正确的语句,我知道传递参数行的 cur.execute 不正确。你能帮我解决这个问题吗,提前感谢。

这是代码

class Searchb:
def __init__(self,isbn,author,title):
self.isbn=isbn
self.author=author
self.title=title
def booksearch(self):
query= "select author,title from books where isbn LIKE '%%s%%' OR author LIKE '%%s%%' OR title like '%%s%%' "
cur.execute(query,(self.isbn,self.author,self.title),)
book=cur.fetchmany()

您正在使用没有任何参数的cur.fetchmany((。 从文档中:

每次调用要提取的行数由参数指定。如果未给出,则游标的数组大小确定要读取的行数。

arraysize 默认为 1,这就是为什么你只得到 1 行。 要么指定更高的迭代,直到不再得到任何结果,要么只使用cur.fetchall()

问题出在 select 语句中,我可以找到自己的正确语法 select 语句与 喜欢 同时您将参数传递给数据库; postgres

query="select author,title from books where isbn LIKE %s or author like %s or title like %s "
book_to_search=(self.isbn,self.author,self.title)
cur.execute(query,book_to_search)
book=cur.fetchall() 

谢谢大家!

最新更新