【问题标题】:Ppython tkinter: No such ColumnPython tkinter:没有这样的专栏
【发布时间】:2022-10-06 17:53:24
【问题描述】:

我想将所有表的名称存储到一个列表中,然后在另一个窗口中打印它。

但是当我尝试将这些名称放入新表时,它给了我:

OperationalError: no such column: and the name of the name i want to insert in

问题主要出在函数openThirdWindow

提前致谢。 这是我的代码:

customtkinter.set_appearance_mode(\"dark\")  
customtkinter.set_default_color_theme(\"blue\")  
k=0
#creare e connettersi al database
conn = sqlite3.connect(\"Clienti.db\")
c = conn.cursor()
#finestra
root = customtkinter.CTk() 
root.geometry(\"600x480\")
root.title(\"Cucine&Cucine app\")
global thirdWindow
#crea finestra per vedere i clienti
thirdWindow = Toplevel()
thirdWindow.config(bg=\"#201C1C\")
thirdWindow.geometry(\"800x500\")
#nascondi la finestra
thirdWindow.withdraw()
#funzioni
#funzione random che serve
def disable_event():
    pass
#disabilitare il bottone X per chiudere la finestra
thirdWindow.protocol(\"WM_DELETE_WINDOW\", disable_event)
#bottone INVIO
def sumbitClick():
    global prezzo
    global contentComponenti
    global componente
    global secondWindow
    global contentPrezzi
    secondWindow = Toplevel()
    secondWindow.config(bg=\"#201C1C\")
    secondWindow.geometry(\"600x700\")
    nComponenti = inputNum.get()
    contentComponenti = []
    contentPrezzi = []
    for i in range(int(nComponenti)):
        labelComponenti= customtkinter.CTkLabel(secondWindow, text=\"componente \" + str(i+1), bg=\"#201C1C\", fg=\"white\")
        labelPrezzo= customtkinter.CTkLabel(secondWindow, text=\"prezzo \" + str(i+1), bg=\"#201C1C\", fg=\"white\")
        labelComponenti.pack()
        componente = customtkinter.CTkEntry(secondWindow)
        prezzo = customtkinter.CTkEntry(secondWindow)
        contentComponenti.append(componente)
        contentPrezzi.append(prezzo)
        componente.pack(pady=2)
        labelPrezzo.pack(pady=2)
        prezzo.pack(pady=1)
        
    buttonInsert = customtkinter.CTkButton(secondWindow, text=\"Fatto\", command=inserisciDati)
    buttonInsert.pack(pady=10)

#Bottone per chiudere la finestra dei clienti
def closeThirdWindow():
    thirdWindow.withdraw()
    
#bottone INVIO(inserisce i dati nel database)
def inserisciDati():
    global tabellaNuova
    global nomiTabelle
    nomiTabelle = []
    #immagazzinare il valore di ogni componente
    valuesComponenti = list(componente.get() for componente in contentComponenti)
    #immagazzinare il prezzo di ogni componente
    valuesPrezzi = (prezzo.get() for prezzo in contentPrezzi)
    #connettersi al database
    conn = sqlite3.connect(\"Clienti.db\")
    c = conn.cursor()
    tabellaNuova = str(inputNome.get())
    nomiTabelle.append(tabellaNuova)
    #crea tabella sql
    c.execute(\"\"\"
        CREATE TABLE IF NOT EXISTS \"\"\" + tabellaNuova + \"\"\"(
            componenti VARCHAR(50)
        );
    \"\"\")
    #inserire componenti nella nuova tabella
    for componente in valuesComponenti:
        c.execute(\"ALTER TABLE \" + tabellaNuova + \" ADD \" + componente + \" text\")
    #inserire prezzi nella nuova colonna del componente
    conta = 0
    for prezzo in valuesPrezzi:
        c.execute(\"INSERT INTO \" + tabellaNuova + \"(\" + valuesComponenti[conta] + \")\" + \"VALUES(\" + prezzo + \")\")
        conta+=1
    conn.commit()
    conn.close()
    secondWindow.destroy()

#Bottone MOSTRA CLIENTI
def openThirdWindow():
    conn = sqlite3.connect(\"Clienti.db\")
    c = conn.cursor()
    c.execute(\"\"\"
        CREATE TABLE IF NOT EXISTS nomiClienti(
            nomi VARCHAR(50)
        );
    \"\"\")
    #inserire componenti nella nuova tabella
    for nome in nomiTabelle:
        c.execute(\"\"\"INSERT INTO nomiClienti (nomi)
                        VALUES (\"\"\"+ nome +\"\"\");\"\"\")
    conn.commit()
    conn.close()
    thirdWindow.deiconify()
#titolo CREA CLIENTE
labelTitolo = customtkinter.CTkLabel(root, text=\"CREA O MODIFICA NUOVO ORDINE\")
labelTitolo.config(font=(\"Courier\", 20))
labelTitolo.pack()

#etichetta e input per Nome
labelNome = customtkinter.CTkLabel(root, text=\"Nome cliente\", bg=\'#201C1C\', fg=\"white\")
labelNome.config(font=(\"Courier\", 15))
labelNome.pack()
inputNome = customtkinter.CTkEntry(root, width=100)
inputNome.pack(pady=5)

#etichetta numero di componenti e input
labelComponenti = customtkinter.CTkLabel(root, text=\"Quanti componenti vuoi inserire?\")
labelComponenti.config(font=(\"Courier\", 15))
labelComponenti.pack()
inputNum = customtkinter.CTkEntry(root, width=40)
inputNum.pack(pady=5)
#bottone di invio
buttonSubmit = customtkinter.CTkButton(root, text=\"INVIO\",command=sumbitClick)
buttonSubmit.pack()

#button vedi clienti
buttonClienti = customtkinter.CTkButton(root, text=\"MOSTRA CLIENTI\", command=openThirdWindow)
buttonClienti.pack(pady=50)

#button chiuidi terza finestra
buttonClose = customtkinter.CTkButton(thirdWindow, text=\"CHIUDI\", width=150, height=40, command=closeThirdWindow)
buttonClose.pack(anchor = \"s\", side = \"bottom\")


conn.commit()
conn.close()
root.mainloop()
  • 你让我们猜测错误在哪里。请更新问题以包含完整的错误回溯消息。

标签: python sql tkinter sql-insert


【解决方案1】:

请注意,对于以下行:

c.execute("""INSERT INTO nomiClienti (nomi)
             VALUES ("""+ nome +""");""")

如果nome有值,例如"Hello",那么最终的SQL语句如下:

INSERT INTO nomiClienti (nomi) VALUES (Hello);

因此它将在表nomiClienti 中查找列名Hello

最好使用占位符反而:

c.execute("INSERT INTO nomiClienti (nomi) VALUES (?);", (nome,))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多