views:

44

answers:

1

I want to create a rollback button in my django project using MySQLdb. I have tried to use commit() and rollback() with InnoDB as database engine, rollback() seems not work because the database was updated even though rollback() was put after commit(). Here is some related lines in python code:

def update(request):

    if 'weight' in request.GET and request.GET['weight']:
        weight = request.GET['weight']
    else:
        return HttpResponseRedirect('/clamplift/')

    if 'realtag' in request.GET and request.GET['realtag']:
        realtag = request.GET['realtag']
    else:
        return HttpResponseRedirect('/clamplift/')

    conn = MySQLdb.Connect(host="localhost", user="root", passwd="", db="scale")
    cur = conn.cursor()
    cur.execute("UPDATE `scale`.`scale_stock` SET `current_weight` = %s WHERE `scale_stock`.`paper_roll_id` = %s", (weight,realtag))
    conn.commit()

    conn.rollback() # I test rollback() function here.

    cur.close()
    conn.close()

    return HttpResponseRedirect('/clamplift/')

Actually I want one button for update data to database and another button to rollback like providing undo function.

Please give me any idea. Thank you very much.

+1  A: 

I have no experience with MySQLdb directly, but most of the time, the rollback method is only good during a transaction. That is, if a transaction is still open, then rollback will undo everything that has happened since the start of the transaction. So when you call commit, you are ending the transaction and can no longer meaningfully call rollback.

In response to question in comments:

Add a datetime field to the model to track revisions. Create another model with the same fields as the original model and a foreign key into to original model's table. When an update occurs to the original model, copy the old values into the new table, set the datetime to NOW and set the foreign key in the revisions table to point at the row being edited before performing the update. You can then provide a list of previous states to revert to by selecting all of the revisions for the row in question and letting the user select based on datetime. Of course you can also provide the user that made the edit as well or other specific information that you think will be useful to the users of your app.

aaronasterling
+1. Aye. Usually one of Rollback or Commit is issued, not both. Also it doesn't make sense to issue a rollback after a commit.
Manoj Govindan
Thank you very much. So, is there any another way to provide undo function?
Protocole
@Protocole See my update for one way to go about doing this.
aaronasterling
@Aaron: your response to the comment showed up as _code_. I have changed it to a paragraph.
Manoj Govindan
@Manoj Govindan Thanks, my edit-fu is weak tonight. @Protocole. my description was a little off. Please reread it.
aaronasterling
@AaronMcSmooth Thanks again for your response, I will try it.
Protocole
@Protocole - alternatively, there's an app for that - http://github.com/etianen/django-reversion
Dominic Rodger