简体   繁体   English

使用 Python 中的 Sqlite3 模块更新表

[英]Updating a Table by using Sqlite3 Module in Python

I am working on a project where i need to update the password column of a user table pointing userid as the primary key, whenever the user reset his/her password.我正在做一个项目,每当用户重置他/她的密码时,我都需要更新将用户 ID 指向主键的用户表的密码列。 I am passing username and password to update_table function based on the values entered by the user from console and below is my code snippet -我根据用户从控制台输入的值将用户名和密码传递给 update_table function,下面是我的代码片段 -

def sql_update_table(conn, username, reset_password):
    c = conn.cursor()
    #value = (username, reset_password)
    #c.execute('''UPDATE user SET password = ? WHERE userid = ? ''', value)
    c.execute('''UPDATE user SET password = reset_password WHERE userid = username''')
    conn.commit()

I tried both case passing values with a tuple as mentioned in # and directly as mentioned without a #.我尝试了使用 # 中提到的元组传递值和直接没有# 中提到的两种情况传递值。 However, for first case, there is no error but the table is not getting updated with the new value of password and for later one i am getting below error -但是,对于第一种情况,没有错误,但表没有使用新的密码值更新,对于后一种情况,我得到以下错误 -

sqlite3.OperationalError: no such column: reset_password sqlite3.OperationalError:没有这样的列:reset_password

Please help me to solve this.请帮我解决这个问题。 Thanks in advance !提前致谢 !

Can you please try replacing你能尝试更换吗

c.execute('''UPDATE user SET password = reset_password WHERE userid = username''')

with

c.execute('''UPDATE user SET password = ? WHERE userid = ? ''', (username,reset_password))

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

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