首页 > 解决方案 > 获取 TypeError:参数 1 必须是 str,而不是在 DB 中搜索时的元组

问题描述

你能帮一个菜鸟吗?尝试在我的 GUI 中搜索和显示数据库中的数据时出现此错误。'''

 search_box = Entry(search_products)
    search_box_get = search_box.get()
    search_box_get = str(search_box_get)

   def search_product_name():
        connection = sqlite3.connect("database.db")
        cursor = connection.cursor()
        # selects everything from the table called Products
        sql = "SELECT * FROM Products WHERE product_name=?", search_box_get

        all_rows = cursor.execute(sql)

        for record in table.get_children():
            table.delete(record)

        for i in all_rows:
            table.insert('', 'end', values=i)
        connection.commit()

'''

这就是我得到的错误

    all_rows = cursor.execute(sql)
TypeError: argument 1 must be str, not tuple

标签: pythonsqlitetkinter

解决方案


sql是一个元组,但cursor.execute()需要一个 SQL 字符串和一个元组/列表参数。

你也应该得到search_boxinside的内容search_product_name()

def search_product_name():
    search_box_get = search_box.get()

    connection = sqlite3.connect("database.db")
    cursor = connection.cursor()
    sql = "SELECT * FROM products WHERE product_name = ?"
    all_rows = cursor.execute(sql, [search_box_get])
    ...

推荐阅读