简体   繁体   中英

Pass Python variable from one function to another to compare it with the variable of a SQL database

I am trying to pass the variable within a field Entry housed in a def to another one when the button is pressed accept me take my variable id written and compare it to the id of my database on problem is that when I try to run it I jump with this error:

obtener() missing 1 required positional argument: 'myid'

My table would be generated this way

micursor.execute("CREATE TABLE alumnos (ID INTEGER PRIMARY KEY AUTOINCREMENT, NOMBRE VARCHAR(20), APELLIDOS VARCHAR(20), PASSWORD VARCHAR(20), COMENTARIOS VARCHAR(100))")

def insertar():
  conexionBBDD = sqlite3.connect("Form")
  micursor = conexionBBDD.cursor()
  nombre = entry_2.get()
  apellidos = entry_3.get()
  password = entry_4.get()
  comentario = entry_5.get("1.0",END)
  alumnoslist= [(nombre,apellidos,password,comentario)]

micursor.executemany(
'INSERT INTO alumnos(ID,NOMBRE,APELLIDOS,PASSWORD,COMENTARIOS) 
VALUES(NULL,?,?,?,?)', alumnoslist)
conexionBBDD.commit()

and this is the variable that I want to pass

def leer():
    id_ventana = Tk()
    id_ventana.geometry('400x80')
    label_6 = Label(id_ventana, text="ID",width=20,font=("bold", 10))
    label_6.place(x=5,y=25)

    entry_6 = Entry(id_ventana)
    entry_6.place(x=120,y=25)
    myid=entry_6.get()
    Button(id_ventana, text='Aceptar',width=8,height=1,bg='gray',fg='white',command=obtener).place(x=280,y=20)

def obtener(myid):

    micursor.execute('SELECT * FROM alumnos WHERE ID = ?',(myid,))

I try this but nothing

MyID= StringVar()

def leer():

id_ventana = Tk()
id_ventana.geometry('400x80')
label_6 = Label(id_ventana, text="ID",width=20,font=("bold", 10))
label_6.place(x=5,y=25)

entry_6 = Entry(id_ventana,textvariable=MyID)

entry_6.place(x=120,y=25)

#micursor.execute('SELECT * FROM alumnos WHERE ID = ?',(entry_6.get(),))

Button(id_ventana, text='Aceptar',width=8,height=1,bg='gray',fg='white',command=obtener).place(x=280,y=20)

def obtener():

strsql = "SELECT * FROM alumnos WHERE ID = 1"
micursor.execute(strsql)
for i in micursor:


    print(MyID)
    print("ID= ", i[0])
    print("Nombre= ", i[1]) 
    print("Apellidos= ", i[2])
    print("Password= ", i[3])
    print("Comentarios= ", i[4])

    MostrarID.set(i[0])
    Nombre.set(i[1])
    Apellidos.set(i[2])
    Password.set(i[3])
    entry_5.insert(1.0,i[4])

Solved to pass the variable you first declare it out of the functions like this:

MyID= StringVar()

Then you declare it global within the class so that it is not static and can change: def leer():

global MyID

En el campo que quieras comparar la id le metes la variable declarada

entry_6 = Entry(id_ventana, textvariable=MyID)

To the button used to compare you add lambda and you pass the field where you enter the id and the frame of the window

Button(id_ventana, text='Aceptar',command= lambda: obtener(entry_6.get(),id_ventana))

In the function used to execute the sql statement you pass

def obtener(MyID,id_ventana):

and comparate

strsql = "SELECT * FROM alumnos WHERE ID = ?"
micursor.execute(strsql,MyID)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM