python - 获取 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
解决方案
sql
是一个元组,但cursor.execute()
需要一个 SQL 字符串和一个元组/列表参数。
你也应该得到search_box
inside的内容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])
...
推荐阅读
- java - RXjava 代码从 1.0 到 2.0 的转换——subscriber.isUnsubscribed() 不可用
- r - R Shiny:Abline 没有出现在箱线图中
- android - 在 Nativescript 插件中实现第三方 Android 库?
- javascript - js文件未加载到节点js服务器中
- c - 停止函数以中断无限递归
- string - MongoDB 大于 numberLong 问题
- python - 使用 python 的数字到字母数字编码器(排列和正则表达式)
- django - 如何修复仅在 Chrome 中出现的“Uncaught SyntaxError: Unexpected number”?
- r - 如何在 R 中对多列条件进行子集化?
- symfony - DqlBuilder 不返回值,但 mysql 做