![](/img/trans.png)
[英]Will creating serializer.Serializer fields saves to the database?
[英]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.