简体   繁体   English

使用sqlite3更新python中的数据库错误

[英]Update database error in python with sqlite3

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 . 我正在尝试执行通过表单更新数据库的功能,但出现此错误: 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

You don't need [clave.get()] at the end of the assignment to sentencia . 你不需要[clave.get()]在分配结束sentencia That's setting the variable to a tuple, not the SQL string. 将变量设置为元组,而不是SQL字符串。

You also don't have enough parameters in your miCursor.execute() call. 您的miCursor.execute()调用中也没有足够的参数。 There are 7 ? 有7个? in the SQL, but only 6 elements in the parameter list. 在SQL中,但在参数列表中只有6个元素。 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). 您需要重复Id1因为它在开头和结尾(但是实际上,不需要设置ID ,因为您只需将其设置为已经具有的相同值即可)。

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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