将值保存在数据库中的功能?根据组合框(可执行代码)的选择而变化的值和行

Function to save values in the database? Values and rows that vary according to the choice of the combobox (executable code)

我很难做足够简单的事情。抱歉,如果我在解释中稍作停留,但我这样做是为了让问题更清楚,即使这是一件简单的事情。我想通过组合框更新数据库中select编辑和显示的值。

我提供一个可执行代码table。我有一个组合框,我从中 select 一个元素在组合框中搜索值(使用 WHERE Choice =?)并将它们显示在文本框中。这些值通过 table 的两个简单行进行 selected,其中行中有单词 selected 在组合框中是选择 1 和选择 2。例如,如果在组合框 I select“选择 1”中,打印包含文本“选择 1”的数据库行的值。这工作正常,没问题。

我很难创建和创建 save 函数来修改值(在文本框中修改它们)并保存它们(或者更好地说更新它们)。困难在于文本框中显示的值会根据组合框中的选择自动变化。所以我应该写一些东西让 Python 理解根据组合框的选择修改和保存哪 table 行。

例如,在屏幕截图的示例中,select从组合框中输入“选择 1”,我在文本框中显示数据 1 和 2(因为在数据库的第 1 行我有哪里选择=?)。我希望能够修改文本框中的数字并将它们保存(或者更好地说“更新”它们)在数据库的同一行 1 中。选择 2 和数据库的第 2 行也是如此。

任何人都可以帮助我使用 save 功能吗?谢谢

执行table代码:

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)

数据库很简单:

CREATE TABLE "table" (
    "id"    INTEGER,
    "number1"   INTEGER,
    "number2"   INTEGER,
    "choice"    TEXT,
    PRIMARY KEY("id")
);

使用类似于用于显示当前值的 SELECT 语句的 UPDATE 语句。

def save():
    selected_value = combo_choice.get()
    number1 = a.get()
    number2 = b.get()
    cursor.execute("UPDATE table SET number1 = ?, number2 = ? WHERE choice = ?", (number1, number2, selected_value))
    conn.commit()
    pass