在SQLite3和Python中进行事务更新

0 投票
1 回答
1211 浏览
提问于 2025-04-16 18:27

我在用户表中更新行(使用sqlite3和Python),代码是

def update_user_by_id(self,id,first_name,last_name,username,password,privilege,email):
    '''Update user in table user based on id on passed parameters'''
    try:
        connection=sqlite3.connect(self.__path_users)
        cursor=connection.cursor()
        cursor.execute('''UPDATE user SET first_name=?,last_name=?,username=?,password=?,privilege=?,email=? where id=?''',first_name,last_name,username,password,privilege,email,id)
        connection.commit()
        connection.close()
        print "Affected: %d", cursor.rowcount
    except sqlite3.Error, e:
        print "Ooops:", e.args[0]

但是我该怎么把这个放进事务里呢?

1 个回答

1

这已经是一个事务了。当你调用 connection.commit() 的时候,这个事务就完成了。如果你想撤销这个事务,只需要把那一行去掉就可以了。

撰写回答