Lbl = tk.Label(rwin,text='Gender',bg=lcol,fg='white',font=('system',17,'bold')).place(x=0,y=400)
r = StringVar()
def clicked(value):
print(r.get())
query = 'INSERT INTO inventory_db.user (gender) VALUE (%s)'
conn.execute(query,r.get())
db.commit()
conn.close()
Radiobutton(rwin, text='Male', variable=r, value='Male', command= lambda: clicked(r.get())).place(x=0,y=450)
Radiobutton(rwin, text='Female', variable=r, value='Female', command= lambda: clicked(r.get())).place(x=200,y=450)
CodePudding user response:
You're not formatting the query properly and beware of sql injection.
Lbl = tk.Label(rwin,text='Gender',bg=lcol,fg='white',font=('system',17,'bold')).place(x=0,y=400)
r = StringVar()
def clicked(value):
print(r.get())
query = 'INSERT INTO inventory_db.user (gender) VALUE (?)'
conn.execute(query, (r.get(),)) # Extra comma to make it a tuple with one element
db.commit()
conn.close()
Radiobutton(rwin, text='Male', variable=r, value='Male', command= lambda: clicked(r.get())).place(x=0,y=450)
Radiobutton(rwin, text='Female', variable=r, value='Female', command= lambda: clicked(r.get())
CodePudding user response:
First why don't you use the value
argument of clicked()
instead of r.get()
because you have already passed r.get()
when executing clicked()
inside those lambdas.
Also the second argument of curosr.execute()
should be a tuple or list:
def clicked(value):
print(value)
query = 'INSERT INTO inventory_db.user (gender) VALUE (%s)'
conn.execute(query, (value,)) # use tuple
db.commit()
conn.close()