How to display on-screen BBDD records in Python?


Hello, I hope so. When I execute in Python the part of the menu runs fine but when I would have to show the data of the person that is in the table, [] appears.

Selecciona una opción para Buscar Paciente
    1 - Por DNI
    2 - Por nombre
    3 - Por edad
    9 - salir
inserta un numero valor >> 1
Ingrese DNI: 40396902


From Patients I call this function that is in


Functions of

def menuListap():
    import os
    print ("Selecciona una opción para Buscar Paciente")
    print ("\t1 - Por DNI")
    print ("\t2 - Por nombre")
    print ("\t3 - Por edad")
    print ("\t9 - salir")

def Lista_Paciente(p):
    while True:

        opcionMenu = input("inserta un numero valor >> ")

        if opcionMenu=="1":
            #POR DNI
            dni = int(input("Ingrese DNI: "))
            p.execute("SELECT * FROM PACIENTES WHERE DNI='dni'")
            paciente = p.fetchall()

        elif opcionMenu=="2":
            #POR NOMBRE
            nombre = str(input("Ingrese nombre: "))
            p.execute("SELECT * FROM PACIENTES WHERE NOMBRE_PACIENTE= '?'", nombre)
            paciente = p.fetchall()

        elif opcionMenu=="3":
            #POR EDAD
            edad = int(input("Ingrese edad: "))
            p.execute("SELECT * FROM PACIENTES WHERE EDAD= '?'", edad)
            paciente = p.fetchall()

        elif opcionMenu=="9":

            print ("")
            input("No has pulsado ninguna opción correcta...\npulsa una tecla para continuar")

At my discretion, the error is found at the moment I try to enter the variable both ID, name and age to the sql code.

PS: if in the part of p.execute("SELECT * FROM PACIENTES WHERE DNI='dni'") instead of putting the variable dni I enter the ID number ( p.execute("SELECT * FROM PACIENTES WHERE DNI='40396902'") ) in if I execute it perfectly but the idea is to be entered by keyboard and not manually.

asked by Gerónimo Membiela 27.12.2018 в 15:21

1 answer


Greetings I suggest you build your sql sentence previously:

dni = int(input("Ingrese DNI: "))
sql = "SELECT * FROM MI_TABLA WHERE dni = '"+str(dni)+"'"
print (sql)

We ask for the ID, then we form the sql statement as shown above, the possible result would be something like this:

Finally you should just put something like this:

dni = int(input("Ingrese DNI: "))
sql = "SELECT * FROM MI_TABLA WHERE dni = '"+str(dni)+"'"
#ponemos sql porque es alli donde tiene mi sentencia a ejecutar

I hope I will help you and help you

Update Try the following sentence:

sql = """SELECT * FROM mi_tabla WHERE dni = '%s' """ % (dni)
answered by 27.12.2018 / 16:00