When I click the refresh button it queries the data from SQL database, but in the list box it duplicate the values
def show():
mysqldb = mysql.connector.connect(host="localhost", user="root", password="1986@", database="ecogreen")
mycursor = mysqldb.cursor()
mycursor.execute("SELECT id,Firstname,Lastname,Mobile,Location FROM customers1")
records = mycursor.fetchall()
for i, (id, Firstname, Lastname, Mobile, Location) in enumerate(records, start=2):
listBox.insert("", "end", values=(id, Firstname, Lastname, Mobile, Location))
mysqldb.close()
I tried to include the clear and delete function, but it is not working, can some one please help me
Warm Regards Janarthanan
CodePudding user response:
You need to clear listBox
before populating data from database:
def show():
mysqldb = mysql.connector.connect(host="localhost", user="root", password="1986@", database="ecogreen")
mycursor = mysqldb.cursor()
mycursor.execute("SELECT id,Firstname,Lastname,Mobile,Location FROM customers1")
records = mycursor.fetchall()
mysqldb.close()
# clear table
listBox.delete(*listBox.get_children())
# populate table
for i, (id, Firstname, Lastname, Mobile, Location) in enumerate(records, start=2):
listBox.insert("", "end", values=(id, Firstname, Lastname, Mobile, Location))