首页 > 解决方案 > 通过 2 个组合组合框提取数据库字段。提取函数中的问题

问题描述

通过选择两个组合框(结合绑定),我想从 Table1 中提取一个字段的 ID 并将其插入到 Table2 中。我根据组合框提取字段没有问题,但我在提取基于 2 个组合框的字段时遇到问题,因为它们是相互组合的。问题只是def id_rounds()函数。

我有两个组合框:一个是我选择“锦标赛”的名称,另一个是我选择回合数(每个锦标赛从 1 到 38 个不同的回合)。要选择锦标赛 ID 必须匹配的锦标赛,我使用组合框 combo_Tournaments 和函数 def combo_tournaments; 而要选择回合的数量,我使用组合框 combo_Rounds 和 combo_rounds 函数。通过选择锦标赛和/或回合,也会自动输入相关 ID(以及实际数据)。所以每个组合框每个放入 2 个东西,总共 4 个。

这是数据库:

CREATE TABLE "All_Tournament" (
    "ID_Tournament" INTEGER,
    "Tournament" TEXT,
    PRIMARY KEY("Tournament" AUTOINCREMENT)
);

    CREATE TABLE "All_Round" (
        "ID_Round"  INTEGER,
        "Number_Round"  INTEGER,
        "ID_Tournament" INTEGER,
        PRIMARY KEY("ID_Round" AUTOINCREMENT),
    );

问题:目前,当我编写函数def id_rounds()的代码时,所选回合的 ID 被保存,但与锦标赛组合框中所选锦标赛没有完全对应。问题是每个锦标赛都由 38 个不同的回合组成,因此在 All_Round 表中,从 1 到 38 的数字会重复多次,每个都对应于锦标赛 ID。比如意甲1到38轮;乙级联赛从 1 轮到 38 轮;英超联赛从 1 轮到 38 轮。所以我想输入锦标赛对应的单轮ID(与锦标赛相关),因为每个锦标赛都有1到38轮,所以每个锦标赛有很多不同的“1到38轮”。

#Combobox Tournament
lbl_Tournament = Label(root, text="Tournament", font=("Calibri", 11), bg="#E95420", fg="white")
lbl_Tournament.place(x=6, y=60)
combo_Tournaments = ttk.Combobox(root, font=("Calibri", 11), width=30, textvariable=campionato, state="readonly")
combo_Tournaments.place(x=180, y=60)
combo_Tournaments.set("Select")
combo_Tournaments['values'] = combo_tournaments()
combo_Tournaments.bind('<<ComboboxSelected>>', combo_teams)

lbl_Rounds = Label(root, text="Rounds", font=("Calibri", 11), bg="#E95420", fg="white")
lbl_Rounds.place(x=600, y=60)
combo_Rounds = ttk.Combobox(root, font=("Calibri", 11), width=30, textvariable=rounds,  state="readonly") 
combo_Rounds.place(x=680, y=60)
combo_Rounds.set("Select")
combo_Rounds['values'] = combo_campionati()
combo_Tournaments.bind('<<ComboboxSelected>>', combo_rounds, add=True)


def combo_tournaments():
    tournaments = combo_tournaments.get()
    cursor.execute('SELECT Tournament FROM All_Tournament')
    result=[row[0] for row in cursor]
    return result

def id_tournaments():
    tournaments = combo_tournaments.get()
    cursor.execute('SELECT ID_Tournament FROM All_Tournament WHERE Tournament=?',(tournaments,))
    result=[row[0] for row in cursor]
    return result[0]

def combo_rounds(event=None):
    rounds = combo_rounds.get()        
    cursor.execute('SELECT Number_Round From All_Round WHERE ID_Tournament')
    result=[row[0] for row in cursor]
    combo_Rounds['value'] = result
    return result

#THE PROBLEM IS HERE 
def id_rounds():
    rounds = combo_rounds.get()    
    cursor.execute('SELECT ID_Round FROM All_Round WHERE Number_Round=? AND Tournament=?',(rounds, tournaments))
    result=[row[0] for row in cursor]
    return result[0]

def combo_teams(event=None):
    tournaments = combo_tournaments.get()
    cursor.execute('SELECT s.Name_Teams FROM All_Teams s, All_Tournament c WHERE s.ID_Tournament=c.ID_Tournament AND c.Tournament = ?', (tournaments,))
    result=[row[0] for row in cursor]
    combo_Teams_1['values'] = result
    combo_Teams_2['values'] = result
    return result

我想得到什么?所以我想获得例如:如果从锦标赛组合框中选择意甲,然后选择第 1 轮,则在结果表中应输入第 1 轮的 ID,但对应于意甲。或者,另一个示例,如果从锦标赛组合框我选择 Serie B,然后选择 Round 1,第 1 轮的 ID 应输入到 Results 表中,但对应于 Serie B。

问题:如何修复函数 def id_rounds 并插入与锦标赛相对应的回合数?目前我只在组合框中输入所选回合的ID,而不匹配在锦标赛组合框中选择的冠军。

标签: pythonsqlpython-3.xsqlitetkinter

解决方案


以下是根据我的理解修改后的代码:

def combo_tournaments():
    cursor.execute('SELECT Tournament FROM All_Tournament')
    result=[row[0] for row in cursor]
    return result

def combo_rounds(event=None):
    # get all Number_Round for selected tournament
    cursor.execute('''
        SELECT Number_Round From All_Round r, All_Tournament t
        WHERE r.ID_Tournament = t.ID_Tournament AND Tournament = ?''', (campionato.get(),))
    result=[row[0] for row in cursor]
    combo_Rounds['value'] = result  # update combo_Rounds
    rounds.set('Select') # reset Rounds selection
    return result

def id_rounds(event=None):
    # get the ID_Round based on selected tournament and Number_Round
    cursor.execute('''
        SELECT ID_Round FROM All_Round r, All_Tournament t
        WHERE r.ID_Tournament = t.ID_Tournament AND Number_Round = ? AND Tournament = ?''',
        (rounds.get(), campionato.get()))
    result = cursor.fetchone()
    if result:
        print(result[0])
        return result[0]
    return None

...

campionato = StringVar()
rounds = StringVar()

#Combobox Tournament
lbl_Tournament = Label(root, text="Tournament", font=("Calibri", 11), bg="#E95420", fg="white")
lbl_Tournament.place(x=6, y=60)
combo_Tournaments = ttk.Combobox(root, font=("Calibri", 11), width=30, textvariable=campionato, state="readonly")
combo_Tournaments.place(x=180, y=60)
combo_Tournaments.set("Select")
combo_Tournaments['values'] = combo_tournaments()
combo_Tournaments.bind('<<ComboboxSelected>>', combo_rounds)

lbl_Rounds = Label(root, text="Rounds", font=("Calibri", 11), bg="#E95420", fg="white")
lbl_Rounds.place(x=600, y=60)
combo_Rounds = ttk.Combobox(root, font=("Calibri", 11), width=30, textvariable=rounds,  state="readonly")
combo_Rounds.place(x=680, y=60)
combo_Rounds.set("Select")
combo_Rounds.bind('<<ComboboxSelected>>', id_rounds)
...

请注意,我已使用campionatoand rounds( StringVar) 来获取选定的锦标赛和Number_Round


推荐阅读