Ppython tkinter: No such Column

Question:

I want to store all table’s names into a list amd then print it in another window.

But when I try to put those names into the new table, it gives me:

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

The problem is mainly in the function openThirdWindow.

Thanks in advance.
Here’s my code:

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()
Asked By: Sassolino

||

Answers:

Note that for the following line:

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

If nome has value, for example, "Hello", then the final SQL statement will be like below:

INSERT INTO nomiClienti (nomi) VALUES (Hello);

So it will look for a column name Hello in table nomiClienti.

It is better to use placeholder instead:

c.execute("INSERT INTO nomiClienti (nomi) VALUES (?);", (nome,))
Answered By: acw1668
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.