繁体   English   中英

function 中的问题,将字段从数据库表中提取并保存到另一个

[英]Problem in a function that extracts and saves fields from a database table to another

如果在 combo_Nations combobox I select 一个特定的国家(国家名称是从“Nations_name”列中的“All_Nations”表中提取的),我想获得相应的所选国家的ID_Nations(ID_Nations在同一个表“All_Nations”)。 单击带有 function def_add 的“添加”按钮后,ID 将与其他字段一起自动插入到数据库的另一个表中。 (如果您想知道为什么我需要从另一个表自动插入 ID_Nation,原因是我需要它用于外键的关系目的)

所以我想从截图中用红色圈出的数据(我不能在这里附上图片): screenshot 基本上表格是这样的:

CREATE TABLE "All_Nations" (
    "ID_Nations"    INTEGER, >>> example: 453
    "Nations_name"  INTEGER, >>> example: England
    PRIMARY KEY("ID_Nations" AUTOINCREMENT)
);

因此,带有 def combo_nations function 的 combobox combo_Nations 仅获取 Nations_name。 而 def id_nations 应该从 combobox 中提取与所选国家对应的 ID_Nations。示例:例如,如果我在 combobox 中的 select 英格兰,id_nations 应该会自动保存我 453。由于 function def,这两个数据将保存在一个新表中add ()(我只写了这段代码的一部分,以免对其进行详细说明,向您展示了解我服务的最低限度,因为它可以正常工作)。 新表将保存一行,其中将有:ID,Nations_name,ID_Nations,其他各种数据。

我得到的错误是这个 TypeError: 'list' object is not callable,准确地说是这样的:

   db.insert(nations.get(),.....other data .get(), id_campionati_value())
TypeError: 'list' object is not callable

不知道是我把def id_nations function写错了,是db.insert的问题还是两者都有

这是代码。 我创建的 def id_nations function 有一些问题:

def id_nations():
    nations = combo_Nations.get()
    cursor.execute('SELECT ID_Nations FROM All_Nations WHERE Nations_name=?',(nations,))
    result = cursor.fetchone()
    return result

#this is ok. no problem
def combo_nations():
    campionato = combo_Nations.get()
    cursor.execute('SELECT Nations_name FROM All_Nations')
    result=[row[0] for row in cursor]
    return result

#Combobox Nations
lbl_Nations = Label(root, text="Nations", font=("Calibri", 11), bg="#E95420", fg="white")
lbl_Nations.place(x=6, y=60)
combo_Nations = ttk.Combobox(root, font=("Calibri", 11), width=30, textvariable=nations, state="readonly")
combo_Nations.place(x=180, y=60)
combo_Nations.set("Select")
combo_Nations['values'] = combo_nations()
combo_Nations.bind('<<ComboboxSelected>>', combo_city)

数据将保存在这里(一切正常):

def add():
    id_campionati_value=id_campionati()
    
    db.insert(nations.get(),.....other data .get(), id_campionati_value())
    messagebox.showinfo("Success", "Record Inserted")
    clearAll()
    dispalyAll()

理论上我明白问题是什么,但我不知道如何解决。 我刚开始使用 Python。你能告诉我答案中的解决方案吗? 谢谢

PS:我删除了不必要的代码部分,以免延长问题的长度,但是您需要了解的一切。 谢谢

更新代码 Rest 以将数据插入数据库。 我以一种有点混乱的方式编写它,因为我有两个文件(main.py 和 db.py)并且代码是分开的。 如果我写这两个文件的所有代码,那就太长了。

def getData(event):
    selected_row = tv.focus()
    data = tv.item(selected_row)
    global row
    row = data["values"]
    #print(row)

    nations.set(row[1])
    ....
    id_nations.set(row[10])



    # Insert Function
    def insert(self, nations,....., id_nations):
        self.cur.execute("insert into All_Nations values (NULL,?,?,?,?,?,?,?,?,?,?)",
                         (nations,..., id_nations))
        self.con.commit()

根据错误, id_campionati_value是一个列表。 当您执行id_campionati_value()时,您正在调用列表,正如错误所说,您无法调用列表。

由于您似乎需要单个值,因此您首先需要定义id_nations以返回 id 而不是列表。 它看起来类似于以下示例,返回匹配行的第一列。

我还建议重命名 function 以使其更清楚地表明它正在从数据库中获取某些内容:

def get_id_nations():
    nations = combo_Nations.get()
    cursor.execute('SELECT ID_Nations FROM All_Nations WHERE Nations_name=?',(nations,))
    result = cursor.fetchone()
    return result[0]

然后,您可以在调用db.insert时使用此值:

id_nations = get_id_nations()
db.insert(nations.get(),.....other data .get(), id_nations)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM