获取 TypeError:参数 1 必须是 str,而不是在 DB 中搜索时的元组
Posted
技术标签:
【中文标题】获取 TypeError:参数 1 必须是 str,而不是在 DB 中搜索时的元组【英文标题】:Getting TypeError: argument 1 must be str, not tuple when searching in DB 【发布时间】:2021-05-03 05:55:42 【问题描述】:你能帮助一个菜鸟吗? 尝试在我的 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
【问题讨论】:
【参考方案1】:sql
是一个元组,但cursor.execute()
需要一个 SQL 字符串和一个元组/列表参数。
您还应该在search_product_name()
中获取search_box
的内容:
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])
...
【讨论】:
谢谢先生!!!你真的帮了我。祝你有美好的一天:)以上是关于获取 TypeError:参数 1 必须是 str,而不是在 DB 中搜索时的元组的主要内容,如果未能解决你的问题,请参考以下文章