python - postgres在python中传递参数的问题
问题描述
我有这个代码,它采用标题、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()
推荐阅读
- python - 输入和结果对象的 Pythonic 解耦
- r - 合并症包:如何通过 ID 计算查尔森合并症指数?
- php - Laravel array_merge():参数 #2 不是数组错误
- post - Python 请求 GET 返回 200 但 POST 返回 404
- c# - C# SoundPlayer 类在指定路径中找不到文件
- mule - 类时间戳上的无效属性
- azure-eventhub - 如何修复 Java 中的“java.lang.NoClassDefFoundError: com/microsoft/azure/eventhubs/EventHubClient$”错误
- javascript - 每次单击按钮时,单击功能会触发更多次
- node.js - 为什么它会给出错误节点 expressjs?
- javascript - 单独的 js 文件导致 Uncaught SyntaxError: Unexpected token {