Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中postgres传递参数的问题_Python_Postgresql - Fatal编程技术网

python中postgres传递参数的问题

python中postgres传递参数的问题,python,postgresql,Python,Postgresql,我有这段代码,它获取书名,或isbn,或一本书的作者,并从数据库中检索所有匹配的数据 问题在于传递参数行,它只检索第一条记录,重新记录用户输入的数据 我试图在数据库控制台中使用select语句,它检索到了正确的语句,我知道传递参数行的cur.execute不正确。您能帮我一下吗?请提前感谢 这是密码 class Searchb: def __init__(self,isbn,author,title): self.isbn=isbn self.author

我有这段代码,它获取书名,或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将确定要提取的行数


arraysize默认值为1,这就是为什么只得到1行。要么指定更高的值进行迭代,直到没有更多结果,要么只使用
cur.fetchall()

问题出在select语句中,我可以在将参数传递到数据库的同时找到select语句的正确语法;博士后

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

谢谢大家

不,我尝试使用fetchall()获得相同的结果。结果不正确。您说它只检索到第一条记录。结果中有什么不正确?检索数据库中的第一条记录或检索所有记录,无论您要的是isbn还是作者姓名或书名??