skip to Main Content

This is part of a much larger project I’m working on, but I’m trying to break it down to the most basic question I could ask. How do I write the following line?

search = "SELECT Description FROM doitbest WHERE PrimaryUPC = UPC_entry.get()"

I’m trying to use Python to search my MySQL database by using CustomTkinter as a GUI. Long story short, I want to scan a barcode into a CTkEntry box (UPC_entry), press the CTkButton that I made, have it pull the barcode from the CTkEntry box, and then use that barcode to search my database for the relevant information. No matter how I try to write the above line, I keep getting errors. It all comes down to the last part "UPC_entry.get()". I don’t know how to get the barcode out of the CTkEntry box (UPC_entry) and use that to search the database. If I replace UPC_entry.get() with a regular barcode (eg. 077922843671), it will come up with the correct information. I just don’t understand what I’m doing wrong. Is .get() even the right thing I should be using? I greatly appreciate any suggestions you guys might have.

2

Answers


  1. Probably the main reason is dtype. If description‘s dtype is int then u should convert it to int. Because u get string value from the entry box.

    Login or Signup to reply.
  2. You cannot put UPC_entry.get() directly in a SQL statement. You need to use placeholder in the SQL statement and pass the required value when you call cursor.execute(...):

    # use placeholder in the SQL statement, %s for MySQL
    search = "SELECT Description FROM doitbest WHERE PrimaryUPC = %s"
    # assume cursor is already created and pass a list/tuple containing the required value
    cursor.execute(search, [UPC_entry.get()])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search