This is the simple code I want to execute. I want the elements of tkinter combobox come from sqlite3 database it does the job but It appends the first row and first column values many times(no of rows in database) . I want it to append the first column of each rows of databse.
con_caster = sqlite3.connect('caster.db')
con_sec = sqlite3.connect('section.db')
cur_caster = con_caster.cursor()
con_sec = con_sec.cursor()
#cur_caster.execute("DROP TABLE IF EXISTS autos;") # use this line only if you want to overwrite existing table
cur_caster.execute("CREATE TABLE IF NOT EXISTS caster ('casterid' TEXT, 'casterradius' NUMBER);")
cur_caster = con_caster.cursor()
query = cur_caster.execute('SELECT casterid FROM caster')
caster_data = []
for row in cur_caster.fetchall():
caster_data.append(row[0])
combo['values'] = caster_data
con_caster.commit()
And this is how I have created the rows in database
def add_to_database():
i=0
# add details to database
caster_data.append((add_cid.get()))
cur_caster.execute("INSERT INTO caster VALUES (?, ?);", (caster_data[i],caster_data[i] ))
con_caster.commit()
combo['values'] = caster_data
i+=1
When I delete the database file and run the app it shows
When I add data, the combobox looks like
After closing and opening app again he combobox looks like
And the database also changes same as tyhe combobox.
Looking for the help in this. Thank you so much in advance