i try to save an integer and it saves as a list, sqlite python

Viewed 23

I have the following problem in this part of my code, I explain: I have a combobox that is filled directly from the database, from there I get the data, then with a button a function is executed to obtain the id of the combobox data and then save the id in the database, the problem is that when doing the sql query to obtain the id it is saved as a list and so it does not work for me I just want the integer to be saved

        def guardar():
            listaId = []
    


        cursor = conexion.cursor()

Here I made this function to make a query using the data I got from the combobox to get the id of each of the tables, but the ids are saved as lists

            for cilindros, empresa, tamano, pico in lista:
                
                cursor.execute("SELECT id FROM empresas WHERE empresa =?", (empresa.get(),))
                empresaId = cursor.fetchall()
                
                
                cursor.execute("SELECT id FROM tamano_precio WHERE tamano =?", (tamano.get(),))
                tamanoId = cursor.fetchall()
                
                
                cursor.execute("SELECT id FROM picos WHERE pico =?", (pico.get(),))
                picoId = cursor.fetchall()
                

                listaId.append([empresaId, tamanoId, picoId])
            
            cursor.close()

Here the id is stored in the database

            cursor2 = conexion.cursor()

            for empresaId, tamanoId, picoId in listaId:
                


                cursor2.execute("INSERT INTO cilindros(empresa_id, pico_id, tamano_id, jefesf_id) VALUES (?, ?, ?, ?)", (str(empresaId), str(picoId), str(tamanoId), str(guardarCedula)))
                conexion.commit()

                

            cursor2.close()

Here are the comboboxes, the data you get are the names of the companies, sizes and peaks

        ventana5 = Toplevel()
        cantidadVacios = cantidadCilindros.get() +1
        lista = []

        for cilindros in range(1, cantidadVacios):

            titulo = tk.LabelFrame(ventana5, text= f"Cilindro {cilindros}")
            titulo.pack()

            empresa = ttk.Combobox(titulo, width=11)
            empresa["values"] = emp()
            empresa.current(0)
            empresa.pack()

            tamano = ttk.Combobox(titulo, width=11)
            tamano["values"] = taman()
            tamano.current(0)
            tamano.pack()

            pico = ttk.Combobox(titulo, width=11)
            pico["values"] = pic()
            pico.current(0)
            pico.pack()


            lista.append([cilindros, empresa, tamano, pico])

        Button(ventana5, text="Guardar", command=guardar).pack()

This is how they are stored: enter image description here

And this is how I want them to be saved: enter image description here

1 Answers

cursor.fetchall() returns a list of all the results of the SELECT query, so that you will know if there is more than 1 entry that matched your WHERE statement, and you will act upon it. while cursor.fetchone() returns only the first result, but in a tuple, so you want to extract the first element in it.

you can either convert your cursor.fetchall() statements to cursor.fetchone()[0] or just take the first element using cursor.fetchall()[0][0], which will be slightly slower than fetchone()[0].

Related