Rollback doesn't work in MySQLdb

Posted by Anton Barycheuski on Stack Overflow See other posts from Stack Overflow or by Anton Barycheuski
Published on 2013-10-19T21:29:34Z Indexed on 2013/10/19 21:55 UTC
Read the original article Hit count: 186

Filed under:
|
|

I have next code

...

db = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db, charset='utf8', use_unicode=True)
db.autocommit(False)
cursor = db.cursor()

...

for col in ws.columns[1:]:
    data = (col[NUM_ROW_GENERATION].value, 1, type_topliv_dict[col[NUM_ROW_FUEL].value])
    fullgeneration_id = data[0]
    type_topliv = data[2]

    if data in completions_set:
        compl_id = completions_dict[data]
    else:

        ...

        sql = u"INSERT INTO completions (type, mark, model, car_id, type_topliv, fullgeneration_id, mark_id, model_id, production_period, year_from, year_to, production_period_url) VALUES (1, '%s', '%s', 0, %s, %s, %s, %s, '%s', '%s', '%s', '%s')" % (marks_dict[mark_id], models_dict[model_id], type_topliv, fullgeneration_id, mark_id, model_id, production_period, year_from, year_to, production_period.replace(' ', '_').replace(u'?.?.', 'nv') )
        inserted_completion += cursor.execute(sql)

        cursor.execute("SELECT fullgeneration_id, type, type_topliv, id FROM completions where fullgeneration_id = %s AND type_topliv = %s" % (fullgeneration_id, type_topliv))
        row = cursor.fetchone()
        compl_id = row[3]

    if is_first_car:
        deleted_compl_rus = cursor.execute("delete from compl_rus where compl_id = %s" % compl_id)

    for param, row_id in params:
        sql = u"INSERT INTO compl_rus (compl_id, modification, groupparam, param, paramvalue) VALUES (%s, '%s', '%s', '%s', %s)" % (compl_id, col[NUM_ROW_MODIFICATION].value, param[0], param[1], col[row_id].value)
        inserted_compl_rus += cursor.execute(sql)

    is_first_car = False

db.rollback()  
print '\nSTATISTICS:'
print 'Inserted completion:', inserted_completion
print 'Inserted compl_rus:', inserted_compl_rus
print 'Deleted compl_rus:', deleted_compl_rus
ans = raw_input('Commit changes? (y/n)')
db.close()

I has manually deleted records from table and than run script two times. See https://dpaste.de/MwMa . I think, that rollback in my code doesn't work. Why?

© Stack Overflow or respective owner

Related posts about python

Related posts about mysql-python