Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python - Getting TypeError: argument 1 must be str, not tuple when searching in DB

Could you please help a noobie. I'm getting this error when trying to search and display data from database in my 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()

'''

That's the error I'm getting

    all_rows = cursor.execute(sql)
TypeError: argument 1 must be str, not tuple
question from:https://stackoverflow.com/questions/65952340/getting-typeerror-argument-1-must-be-str-not-tuple-when-searching-in-db

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

sql is a tuple but cursor.execute() requires a SQL string and a tuple/list arguments.

Also you should get the content of 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])
    ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...