Ruben Oldenkamp
Ruben Oldenkamp

Reputation: 223

AttributeError: 'MySQLCursor' object has no attribute 'commit'

def fillblast(sequentie, titel_lijst, score_lijst, e_lijst, iden_lijst, pos_lijst, gaps_lijst): 
    conn = mysql.connector.connect(host = "ithurtswhenip.nl", user = "pg2", password = "pg2", database= "pg2", port= "3307") 
    cursor = conn.cursor()
    Blast = 1000
    for i in range(0,len(titel_lijst)):
        Blast =+ 2
        cursor.execute("INSERT INTO `pg2`.`Blast` (`Blast_id`, `Blast_seq`, `Blast_titel`, `Blast_score`, `Blast_E`, `Blast_gaps`, `Blast_pos`, `Blast_iden`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s);", (Blast, sequentie[i] ,titel_lijst[i], score_lijst[i], e_lijst[i], iden_lijst[i], pos_lijst[i], gaps_lijst[i]))
        print("1 record toegevoegd")
    cursor.commit()
    cursor.close() 
    conn.close()

I get the following error:

AttributeError: 'MySQLCursor' object has no attribute 'commit'

How does it come, and where does it go wrong? I try to connect with MySQLWorkbench.

EDIT:

Now I get the following error:

mysql.connector.errors.DatabaseError: 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

Upvotes: 18

Views: 59341

Answers (2)

hlongmore
hlongmore

Reputation: 1846

While fixing some legacy code (that apparently hasn't been working for a couple of years, so users stopped trying to use it), we ran into the same error, using the MySQL-python package in Django. Using the suggestions on this and other answers however resulted in a different error, on account of it occurring within the Django ORM:

django.db.transaction.TransactionManagementError: This code isn't under transaction management

So for those who run into this error after using conn.commit() instead of cursor.commit(), you may be able to use enter_transaction_management and leave_transaction_management (note that this was for Django 1.4.6 and MySQL-python 1.2.5; I may have to update this once we get the Django upgrade completed):

    try:
        conn.enter_transaction_management()
        cursor = conn.cursor()
        cursor.execute(sql)
        conn.commit()
    except DatabaseError as e:
        cursor.rollback()
        log.warning('log warning here')
    # Handle other exceptions here.
    finally:
        if cursor:
            cursor.close()
        conn.leave_transaction_management()

Upvotes: 1

Mp0int
Mp0int

Reputation: 18727

Because you can not commit a cursor! you must commit the connection.

# cursor.commit() --> This is wrong!
conn.commit()  # This is right

Check the docs...

Upvotes: 62

Related Questions