Varun
Varun

Reputation: 137

Is it possible to rollback sqlite3 changes after commit in python

I'm calling these functions via tkinter button click. So once the commit is done, if i need to revert changes I'm calling doing rollback but its not reverting the changes

def insert(_id,name,phone):
              conn = sqlite3.connect(db)
              print ("Opened database successfully");
              print (name,phone)
              conn.execute("INSERT INTO VARUN (ID,NAME,PHONE) \
                    VALUES (?,?,?)",(_id,name,phone));
              conn.commit()
              print ("New record(s)" , name," has just been added to the database");
              conn.close()


def rollback():

    conn = sqlite3.connect(db)
    conn.rollback()
    print ("\n changes reverted")
    conn.close()

Upvotes: 1

Views: 3636

Answers (1)

CL.
CL.

Reputation: 180040

This is not possible. Once a transaction has committed, all changes are merged in the database, and are indistinguishable from other data.

If you want to revert changes later, you have to store not only the changed result but also enough information to restore the changes in the database. See Automatic Undo/Redo Using SQLite for an example.

Upvotes: 4

Related Questions