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
Mp0int
  • 18,172
  • 15
  • 83
  • 114
Ruben Oldenkamp
  • 223
  • 1
  • 2
  • 6
  • Oh, I have use conn.close() not cursor.close() – Ruben Oldenkamp Jun 15 '15 at 09:45
  • You can always edit your post – vaultah Jun 15 '15 at 09:47
  • 2
    You must use `conn.commit()` not `cursor.commit()` – Mp0int Jun 15 '15 at 09:49
  • Now I get the following error: "mysql.connector.errors.DatabaseError: 1205 (HY000): Lock wait timeout exceeded; try restarting transaction" – Ruben Oldenkamp Jun 15 '15 at 10:07
  • Check [here](http://stackoverflow.com/questions/5836623/getting-lock-wait-timeout-exceeded-try-restarting-transaction-even-though-im) 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. – Mp0int Jun 15 '15 at 12:02

2 Answers2

59

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

Mp0int
  • 18,172
  • 15
  • 83
  • 114
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()
hlongmore
  • 1,603
  • 24
  • 28