0

I'm trying to do a function that updates a database through a form but I'm getting this error: ValueError: operation parameter must be str.

I have no problem with other functions like delete, read or insert and I don´t know how to fix this problem. I put down two versions of function update and respective errors.

I'm trying this:

def actualizaDatos():

    Id1 = clave.get()
    nombre1 = nombre.get()
    password1 = password.get()
    apellido1 = apellido.get()
    direccion1 = direccion.get()
    comentarios1 = comentarios.get()


    sentencia="UPDATE DATOSUSUARIOS SET ID= ?, NOMBRE_USUARIO = ?, PASSWORD = ?, APELLIDO = ?, DIRECCION = ?, COMENTARIOS = ? WHERE ID=?", [clave.get()]

    miCursor.execute(sentencia, [Id1, nombre1, password1, apellido1, direccion1, comentarios1])

I expected the database to update but I get this error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\John\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "Practica_Guiada.py", line 148, in actualizaDatos
    miCursor.execute(sentencia, [Id1,nombre1, password1, apellido1, direccion1, comentarios1])
ValueError: operation parameter must be str
4
  • You're setting sentencia to a tuple because you have , [clave.get()] at the end of the assignment. What is that for? Commented Jul 7, 2019 at 9:50
  • One of the query parameters passed to the database in cursor.execute() is not of type String although it is required by the database. Have a look at the table definition and at the actual values that are passed to the query and see if the types are valid. Commented Jul 7, 2019 at 9:52
  • @jnns it's not the parameters, it's the query string itself which they have made into a tuple for some reason. SQLite doesn't have any type enforcement for primatives Commented Jul 7, 2019 at 9:53
  • , [clave.get()] remove this from sentencia Commented Jul 7, 2019 at 9:54

1 Answer 1

1

You don't need [clave.get()] at the end of the assignment to sentencia. That's setting the variable to a tuple, not the SQL string.

You also don't have enough parameters in your miCursor.execute() call. There are 7 ? in the SQL, but only 6 elements in the parameter list. You need to repeat Id1 because it's at the beginning and end (but actually, there's no need to set ID, since you'd just be setting it to the same value it already has).

def actualizaDatos():

    Id1 = clave.get()
    nombre1 = nombre.get()
    password1 = password.get()
    apellido1 = apellido.get()
    direccion1 = direccion.get()
    comentarios1 = comentarios.get()


    sentencia="UPDATE DATOSUSUARIOS SET NOMBRE_USUARIO = ?, PASSWORD = ?, APELLIDO = ?, DIRECCION = ?, COMENTARIOS = ? WHERE ID=?"

    miCursor.execute(sentencia, [nombre1, password1, apellido1, direccion1, comentarios1, Id1])
Sign up to request clarification or add additional context in comments.

2 Comments

Thaks again Barmar, but, why i have to put Id1 parameter at last position? If i put it the first one doesnt work.
Variables have to be in the same order as the corresponding ? in the query. WHERE ID = ? is at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.