Firebird 和 Python 中的多个 AND 运算符

Multiple AND operators in Firebird and Python

这是我正在尝试做的示例代码 - 能够在两个以上的输入框和两个以上的数据库列中分别搜索用户输入:

cur.execute("""SELECT * 
FROM table
WHERE col1 LIKE ? AND col2 LIKE ? AND col3 LIKE ?;""", (
"%" + self.col1.get() + "%",
"%" + self.col2.get() + "%", 
"%" + self.col3.get() + "%",))

records = cur.fetchall()
        # Fill TreeView
  print_records = ''
  for record in records:
    print_records += str(record) + "\n"
    self.tree.insert("", END, values=record)

conn.commit()
conn.close()

问题是此代码在我添加 AND col3 LIKE ? 之前正常工作,之后显示的结果不正确。这是为什么?

如果您希望“用户能够根据 一个 框中的条目进行搜索,则必须在查询中使用 OR 而不是 AND”。当前查询正在 all 个框中搜索条目。

所以我解决了这样的问题:总的来说,搜索功能在每次搜索时都会填写其中一列。因此,我在 python 中做了一些 IF 语句来读取该框,并根据 SQL 查询显示结果的条目:

values = [self.col1.get(), 
               self.col2.get(), 
               self.col3.get()]

        if values != '':
            if values[1] or values[0] != '':
                cur.execute("""SELECT * FROM table WHERE col1 LIKE ? AND col2 LIKE ?;""", ("%" + values[0] + "%", "%" + values[1] + "%", ))
            if values[2] != '':
                cur.execute("""SELECT * FROM table WHERE col3 LIKE ? AND col2 LIKE ?;""", ("%" + values[2] + "%", "%" + values[1] + "%", ))
        else:
            pass

代码有效并显示了我和用户尝试获取的结果。谢谢大家的帮助。