How can I pass the value of a variable to the update function of sqlite3 in python

1

I would like to know how to pass data to the update function of sqlite 3:

this is the code:

conexion = sqlite3.connect("Db/Users.db")
cursor = conexion.cursor()
intem = self.lineEdit.text()

cursor.execute('UPDATE Usuarios SET Contraseña ='intem' WHERE ID = 1')

conexion.commit()
conexion.close
    
asked by Revsky01 27.07.2018 в 20:53
source

1 answer

2

This uses a structure similar to the .format() for the string. Specifically, for sqlite3, ? is used as the value to be replaced by data.

Example:

cursor.execute('UPDATE Usuarios SET Contraseña = ?, Otracosa = ? WHERE ID = 1',
              (intem, otracosa))

The first parameter of execute is the query to the database, the second parameter is a Tuple with our data replacing each ? . It is important to mention that being a mandatory tuple, if you only want to pass a single value you have to continue doing it in a Tuple:

cursor.execute('UPDATE Usuarios SET Contraseña = ? WHERE ID = 1', (intem,))
    
answered by 27.07.2018 / 23:17
source