简体   繁体   中英

How can I search in a database using an integer id variable?

I'm developing a CRUD graphic interface.

def crudRead():
myConex=sqlite3.connect("Users")
myCursor=myConex.cursor()
myCursor.execute("SELECT*FROM USER_DATA WHERE ID="+id_number.get())
theUser=myCursor.fetchall()
for user in theUser:
    id_number.set(usuario[0])
    username.set(usuario[1])
    password.set(usuario[2])
    biographyText.insert(1.0, usuario[3])

myConex.commit()

Error: myCursor.execute("SELECT*FROM USER_DATA WHERE ID="+id_number.get())

TypeError: can only concatenate str (not "int") to str


How can i do it to search by the id without having that error? I've tryed to set id_number.get() and it doesn't return any error but the program doesn't work properly because ID column in database have integer values.

Use placeholder:

myCursor.execute("SELECT * FROM USER_DATA WHERE ID=?", (id_number.get(),))

Maybe using f strings could help.

myCursor.execute(f'SELECT*FROM USER_DATA WHERE ID={id_number.get()}')

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