我正在尝试使用Python和SQLite更新用户详细信息。
目标是一次性升级用户的所有列。
我的代码是:
def update():
new_username = input("Input your NEW username:\n")
new_firstname = input("Input your NEW firstname:\n")
new_lastname = input("Input your NEW lastname:\n")
new_email = input("Input your NEW email:\n")
new_password = input("Input your NEW password:\n")
update_customer =("""UPDATE customer SET username = ? AND firstname = ? AND lastname = ? AND email = ? AND password = ?""")
cursor.execute(update_customer, [(new_username), (new_firstname), (new_lastname), (new_email), (new_password)])
我在运行python函数之前和之后检查了数据库。但是,所做的更改不会保存到数据库中。除了用户名消失外,什么都没有改变。
答案 0 :(得分:1)
您无需使用AND来设置其他列。而是用逗号分隔要设置的列。
所以你想要
update_customer =("""UPDATE customer SET username = ?, firstname = ?, lastname = ?, email = ?, password = ?""")
,如果没有将所有行设置为相同的值,则使用WHERE子句。
按照:-
答案 1 :(得分:1)
完成交易后,您需要保存更改。
cursor = conn.cursor() # Get cursor
cursor.execute(...) # Execute some SQL queries
# This is the line you've missed.
# You need to call this function every time you update the data in database.
cursor.commit()
此外,“ UPDATE”命令的SQL语法不正确。指定要更改的多个列时,请使用逗号而不是“ AND”。像这样:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;