18
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
5
  • Oh, I have use conn.close() not cursor.close() Commented Jun 15, 2015 at 9:45
  • You can always edit your post Commented Jun 15, 2015 at 9:47
  • 2
    You must use conn.commit() not cursor.commit() Commented Jun 15, 2015 at 9:49
  • Now I get the following error: "mysql.connector.errors.DatabaseError: 1205 (HY000): Lock wait timeout exceeded; try restarting transaction" Commented Jun 15, 2015 at 10:07
  • Check here for info about database timeout. Probably your titel_lijst is too long and inserting too many rows which consumes a lot of time, more than the MySql transaction timeout limit, so your connection time-out. Commented Jun 15, 2015 at 12:02

2 Answers 2

62

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...

Sign up to request clarification or add additional context in comments.

1 Comment

I am using, connection.commit() but getting same error
1

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.