How to load MySQL info in Tkinter Entries by double-clicking the Listbox element consisting of two columns

1

In this GUI there are some Entries that what they do is allow to enter information to be stored in a MySQL database. The Listbox basically displays one of the fields in the database (the name), which in fact is one of the Entry.

In addition, when double clicking on one of the elements of the Listbox all the Entry are loaded with the corresponding information of the database. But, what I'm trying to do is show the name next to the last name, and when double-clicking on one of them, do the same action: load the information according to each Entry field.

This is the code that loads the names in the Listbox. Reviewed is the option to display the names and surnames:

def cargar_lista():
    try:
        connect.commit()
        display = "SELECT p_nombres, p_apellidos FROM propietarios order by p_nombres"
        cursor.execute(display)
        registros = cursor.fetchall()
        lb.delete(0, END)
        for item in registros:
            nombres = item[0]
            apellidos = item[1]
            #lb.insert(END, nombres)
            lb.insert(END, nombres + ' ' + apellidos)
    except:
        showerror ("Mensaje", "Error al cargar los propietarios.")

And this is part of the code that loads the information in the Entries:

def llenar_campos():
    i = lb.curselection()[0]
    valor = lb.get(i)
    edit = """SELECT * FROM propietarios WHERE p_nombres=("%s");""" % (valor)
    cursor.execute(edit)
    result = cursor.fetchall()
    connect.commit()
    for item in result:
        d1 = item[1] #CC/nit
        d2 = item[2] #título
        d3 = item[3] #Fingreso
        d4 = item[4] #razón social
        d5 = item[5] #Ciudad
        d6 = item[6] #Nombres
        d7 = item[7] #Apellidos
        d8 = item[8] #Dir casa
        d9 = item[9] #Teléfono
        d10 = item[10] #Dir oficina
        d11 = item[11] #Tel oficina
        d12 = item[12] #Fax
        d13 = item[13] #Email

        cedula.set(d1)
        titulo.set(d2)
        ingreso.set(d3)
        rsocial.set(d4)
        residencia.set(d5)
        nombres.set(d6)
        apellidos.set(d7)
        direccion.set(d8)
        telefono.set(d9)
        oficina.set(d10)
        tel.set(d11)
        telfax.set(d12)
        correo.set(d13)

        bloquear() #Bloquea los Entries
    
asked by Einnerlink 25.05.2017 в 00:05
source

1 answer

0

If I have understood you, what you want is that having a ListView that contains a list of complete names (name + surnames) get the names and surnames of the owner selected in the ListView separately to be able to make a query in the bd with that data. You have several options:

  • Use a separator between the name (or names) and the surnames to later pause without problems using split . Typically a comma is used:

    lb.insert(END, nombres + ', ' + apellidos)
    

    This would leave you a ListBox something like this:

      

    PAOLA, SEPÚLVEDA MENDEZ
      ROSA ELENA, GARCÍA LÓPEZ

    Generally the order is usually the reverse: SEPÚLVEDA MENDEZ, PAOLA

    Later you can separate names and surnames using split :

    i = lb.curselection()[0]
    noms, apells = lb.get(i).split(', ')
    cursor.execute("SELECT * FROM propietarios WHERE p_nombres = %s AND p_apellidos = %s", (noms, apells))
    
  • If this does not convince you, you could build a dictionary where the key is the name as it appears in your ListBox and the values are tuples or dictionaries containing the separated name and surname:

    _propietarios = dict() #Variable/atributo global que contendrá los nombres
    
    def cargar_lista():
        try:
            connect.commit()
            cursor.execute("SELECT p_nombres, p_apellidos FROM propietarios order by p_nombres")
            registros = cursor.fetchall()
            lb.delete(0, END)
            for item in registros:
                nombres = item[0]
                apellidos = item[1]
                nombre_completo = nombres + ' ' + apellidos
                _propietarios[nombre_completo] = [nombres, apellidos]
                lb.insert(END, nombre_completo)
        except:
            showerror ("Mensaje", "Error al cargar los propietarios.")
    

    Now at the moment of obtaining the data to pass the query to the database we do not use valor = lb.get(i) simply, but we use the dictionary:

    i = lb.curselection()[0]
    valor = lb.get(i)
    noms, apells = _propietarios[valor] #<<<<<<<<<<<<<<<<<<
    cursor.execute("SELECT * FROM propietarios WHERE p_nombres = %s AND p_apellidos = %s", (noms, apells))
    

I recommend you not to build your query commands to the database as raw python str variables, let alone use the Python methods to format them % , str.format() , etc. You expose your database to injection attacks. I mean to use:

cursor.execute("SELECT * FROM propietarios WHERE p_nombres = '%s'", (valor))

instead of:

edit = """SELECT * FROM propietarios WHERE p_nombres=("%s");""" % (valor)
cursor.execute(edit)

On the other hand, we presuppose that there can not be at any time two people with the same names and surnames, that is, there can not be two PAOLA SEPÚLVEDA MENDEZ.

    
answered by 25.05.2017 / 01:16
source