首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ppython tkinter:没有这样的列

Ppython tkinter:没有这样的列
EN

Stack Overflow用户
提问于 2022-05-22 16:51:14
回答 1查看 81关注 0票数 0

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

但是当我试图把这些名字放到新的桌子上时,它给了我:

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

问题主要发生在函数openThirdWindow上。

提前谢谢。这是我的密码:

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-26 06:13:17

请注意,对于以下一行:

代码语言:javascript
复制
c.execute("""INSERT INTO nomiClienti (nomi)
             VALUES ("""+ nome +""");""")

如果nome有值(例如,"Hello" ),那么最后的SQL语句如下所示:

代码语言:javascript
复制
INSERT INTO nomiClienti (nomi) VALUES (Hello);

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

最好使用占位符来代替:

代码语言:javascript
复制
c.execute("INSERT INTO nomiClienti (nomi) VALUES (?);", (nome,))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72339568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档