Reputation: 11
I am trying to have two ttk comboboxes that fetch data from a sqlite database. I managed to be able and list items in one combobox named divisiondropmenu using the combo_division() function. Now I am trying to do a similar thing for the second combobox named productdropmenu, but the list would need to be narrowed down based on the first selected item in combobox named divisiondropmenu.
For the SQL query to pass the needed information for the second combobox I was thinking of something of this sort:
'SELECT * FROM productname WHERE divisionname=?', (function_name())
Code for the two comboboxes so far:
from tkinter import *
from tkinter import ttk
from tkinter.ttk import *
import sqlite3
def combo_division():
conn = sqlite3.connect('anglingdatabase.db')
cursor = conn.cursor()
cursor.execute('SELECT divisionname FROM productdivision')
data = []
for row in cursor.fetchall():
data.append(row[0])
return data
root = Tk()
root.title("Random Window Title")
root.geometry('1280x720')
rows = 0
while rows < 50:
root.rowconfigure(rows, weight=1)
root.columnconfigure(rows, weight=1)
rows += 1
divisiondropmenu = ttk.Combobox(root)
divisiondropmenu['values'] = combo_division()
divisiondropmenu.bind('<<ComboboxSelected>>', selecteddivision)
divisiondropmenu.grid(row=1, column=2, sticky='NESW')
productdropmenu = ttk.Combobox(root)
productdropmenu['values'] = combo_product()
productdropmenu.grid(row=2, column=2, sticky='NESW')
root.mainloop()
Upvotes: 1
Views: 2017
Reputation: 3907
data = combo_division()
example_sql = ''' SELECT * FROM tablename WHERE division in ('{}') '''.format("','".join(data))
You could build the actual SQL dynamically provided that the data was never user input (to avoid SQL injection).
Upvotes: 0