Absence of the first record in the table when filling a Treeview with the result of a query

1

I am still a novice in Python and I have a problem that has not been solved. I am creating a form to feed / view / modify ... records from a database in PostgreSQL and among other options I want to dump the data from the database to a Treeview ( tabla1 ) to see them as a table.

The fact is that as is my code works well, but with an incidence, and that is that I omits the first line of the database . I've been trying to locate the error for days and I do not know where it is.

This is the code:

def vertablaentera():
    tabla=cajan.get()
    while tabla!="familia" and tabla!="telefonos":
        tkMessageBox.showerror("Administrador","La tabla introducida es erronea")
        cajan.delete(0,END)
        break
    if tabla=="familia":
        cursor.execute("SELECT * FROM familia")
        verRegistros=cursor.fetchone()
        #la linea anterior utiliza fetchone y no fetchall porque el bucle de abajo recorre uno a uno los registros
        if not verRegistros:
            tkMessageBox.showinfo("Administrador","No existen datos")
        else:
            #tkMessageBox.showinfo("INFORMACIO SOLICITADA",verRegistros)
            id=0
            for row in cursor:
                tabla1.insert("",index="end",text=str(id),value=(row[1],row[2],row[3]))
                id+=1
    
asked by Ivan 11.12.2017 в 20:02
source

1 answer

0

If you iterate over cursor totally or partially with for , or use fetchone / fetchmany / fetchall "consume" those elements, so that a subsequent iteration will start where the previous one was and it will only return the remaining results. When you do verRegistros=cursor.fetchone() you are consuming the first result of the query, so that the for next begins with the second row.

There are several ways to solve this, from allowing the scroll on the cursor, use rowcount to know if the query returns without data (although it is not always possible), first fill the treeview and then check if it is empty , use fetchall , etc. However, I think the simplest option is to add this row to treeview before for :

def vertablaentera():
    tabla=cajan.get()
    while tabla!="familia" and tabla!="telefonos":
        tkMessageBox.showerror("Administrador","La tabla introducida es erronea")
        cajan.delete(0,END)
        break
    if tabla=="familia":
        cursor.execute("SELECT * FROM familia")
        verRegistros=cursor.fetchone()
        #la linea anterior utiliza fetchone y no fetchall porque el bucle de abajo recorre uno a uno los registros
        if not verRegistros:
            tkMessageBox.showinfo("Administrador","No existen datos")
        else:
            #tkMessageBox.showinfo("INFORMACIO SOLICITADA",verRegistros)

            tabla1.insert("", index="end", text="0",
                          value=(verRegistros[1], verRegistros[2], verRegistros[3]))

            id=1
            for row in cursor:
                tabla1.insert("", index="end", text=str(id),
                              value=(row[1], row[2], row[3]))
                id += 1
    
answered by 11.12.2017 / 23:53
source