【问题标题】:I try to update data in my sqlite3 address book python program, but the function cannot update my data我尝试在我的 sqlite3 通讯录 python 程序中更新数据,但该函数无法更新我的数据
【发布时间】:2021-10-21 17:19:10
【问题描述】:

我尝试在我的 sqlite3 通讯簿 python 程序中更新数据,因此我输入了这个函数。但是,该功能无法更新我的数据。没有语法错误,但代码不起作用。它说“在“设置”附近有语法错误。谁能帮我解决这个问题?谢谢。

def update_contact():
    name = input('update which name?')
    a = input('phone')
    b = input('address')
    results = cur.execute("UPDATE contact_book set phone = ? set address = ? WHERE name = ?", 
                (a, b, name,))
#cur = con.cursor(), con = sq.connect('contact_book.db')
    view_contact()

【问题讨论】:

    标签: python sqlite syntax


    【解决方案1】:

    每当您更改内容时,您都需要将更改提交到数据库,以便保存

    results = cur.execute("UPDATE contact_book set phone = ? set address = ? WHERE name = ?", (a, b, name,))
    
    con.commit()
    

    【讨论】:

    • 谢谢,我已经添加了,但是现在它返回一个语法错误,我不知道我的代码有什么问题。
    • @Becky1122 尝试像这样删除,(a, b, name)
    【解决方案2】:

    UPDATE 查询中应该只有一个 SET 关键字:

    UPDATE contact_book set phone = ?, address = ? WHERE name = ?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 2017-01-25
      • 1970-01-01
      相关资源
      最近更新 更多