I've tried by selecting one of options from OptionMenu to trigger specific MYSQL query to populate Tree! First time app is running it is OK and it does pull everything, it passes variables to build a query:
SELECT * FROM awp where awp.Order > 1 and awp.date > "1900-01-01" and awp.markerid like "%"
Then it should pass new variables when I select something from OptionMenu. It does, as query is for example:
SELECT * FROM awp where awp.Order > 1 and awp.date = "2004-01-20" and awp.markerid like "%"
or:
SELECT * FROM awp where awp.Order = 140526 and awp.date > "1900-01-01" and awp.markerid like "MARK_X4"
after selecting some of items from OptionMenus, but Treeview is not populated by new query, and it pulls everything from MYSQL db!
This is my first app in python so except trying to change when function is called, how variables are formatted(I had problem with date formatting) I didn't know what else to try. As by going step by step I see that query is updated, but not executed further and Tree is not populated accordingly.
def db_refreshPLAN(self,forder, fdate, fmarker):
if fdate.get() == 'All':
querydate = '1900-01-01'
dateoperator = '>'
else:
querydate = fdate.get()
dateoperator = '='
if fmarker.get() == 'All':
querymarker = '%'
else:
querymarker = fmarker.get()
if forder.get() == 'All':
queryorder = str(1)
orderoperator = '>'
else:
queryorder = forder.get()
orderoperator = '='
dbAWP = mdb.connect("localhost", port=3306, user="root", passwd="Ceres", db="sqltest1")
cursor = dbAWP.cursor()
strquery = str('SELECT * FROM awp where awp.Order ' + orderoperator +
' ' + queryorder + ' and awp.date ' + dateoperator +
' "' + querydate + '" and awp.markerid like "' + querymarker + '"')
print(strquery)
cursor.execute(strquery)
dbAWP.commit()
rows = cursor.fetchall()
cpt = 0
for row in rows:
self.treeplan.insert('', 'end', text=str(cpt), values=(row[1], row[2], row[3], row[4], row[5], row[6],
row[7], row[8], row[9], row[10], row[11], row[12],
row[13], row[14]))
cpt += 1
I wanted to filter Treeview object by executing new SQL query each time one of OptionMenu items is selected.
Ok, sorry for taking anyone's time. I found answer myself by simply "clearing" entire Treeview before "populating" it with results from new query!
Loop actually tried to add "filtered" results in already populated Treeview instead of creating entirely new filtered list!
So...
for i in self.treeplan.get_children():
self.treeplan.delete(i)