'Function to save values in the database? Values and rows that vary according to the choice of the combobox (executable code)
I have a hard time doing something simple enough. Sorry if I dwell a little in the explanation, but I do it to make the question clear even if it is a simple thing. I would like to update the selected and displayed values in the database by means of a combobox.
I present an executable code. I have a combobox from which I select an element which searches for values in the combobox (using WHERE Choice =?) And displays them in the textboxes. The values are selected through two simple rows of a table in which in the lines there is the word selected in the combobox which is Choice 1 and Choice 2. For example, if in the combobox I select "Choice 1", the values of a row of the database are printed which contains the text "Choice 1". This works correctly, no problem.
I have difficulty in creating and creating the save function to modify the values (modifying them inside the textbox) and saving them (or better to say update them). The difficulty is that the values shown in the textboxes vary automatically depending on the choice in the combobx. So I should write something to make Python understand which table row to modify and save depending on the combobox choice.
For example, in the example of the screenshot, selecting "Choice 1" from the combobox, I display the data 1 and 2 in the textboxes (because in row 1 of the database I have the WHERE Choice =?). I would like to be able to modify the numbers in the textoboxes and save them (or better say "update" them) in the same row 1 of the database. Same thing for Choise 2 and row 2 of the database.
Can anyone help me with the save function? Thank you
Executable code:
import sqlite3
from tkinter import ttk
import tkinter as tk
from tkinter import *
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
root.geometry('300x200')
root.config(bg="gray")
root.state("normal")
conn = sqlite3.connect('....')
cursor = conn.cursor()
#VIEW COMBOBOX'S ELEMENT
def combo():
cursor.execute('SELECT choise FROM table')
values = [row[0] for row in cursor]
return values
combo_choice=ttk.Combobox(root, width = 21)
combo_choice.place(x=10, y=10)
combo_choice.set("Select")
combo_choice['value'] = combo()
a = Entry(root,width = 4)
a.place(x=10, y=70)
b = Entry(root,width = 4)
b.place(x=60, y=70)
#RETRIEVE AND PRINT
def get_values(event):
selected_value = combo_choice.get()
cursor.execute('SELECT number1, number2 FROM table WHERE choice=?', (selected_value,))
value = cursor.fetchone()
if value:
#clean
a.delete("0", tk.END)
b.delete("0", tk.END)
#insert
a.insert(tk.END, value[0])
b.insert(tk.END, value[1])
time.bind("<<ComboboxSelected>>", get_values)
#SAVE THE CHANGES
def save():
pass
button = Button(root, text="Save", command= save)
button.place(x=10, y=150)
The database is simply:
CREATE TABLE "table" (
"id" INTEGER,
"number1" INTEGER,
"number2" INTEGER,
"choice" TEXT,
PRIMARY KEY("id")
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

