Python AttributeError: 'MySQLCursor' 对象没有属性 'commit'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30842031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:04:02  来源:igfitidea点击:

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

pythonmysql

提问by Ruben Oldenkamp

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.

它是怎么来的,哪里出错了?我尝试连接 MySQLWorkbench。

EDIT:

编辑:

Now I get the following error:

现在我收到以下错误:

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

回答by FallenAngel

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

检查文档...

回答by hlongmore

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 中使用 MySQL-python 包。然而,使用有关此答案和其他答案的建议会导致不同的错误,因为它发生在 Django ORM 中:

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

django.db.transaction.TransactionManagementError:此代码不在事务管理之下

So for those who run into this error after using conn.commit() instead of cursor.commit(), you may be able to use enter_transaction_managementand 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):

因此,对于那些在使用 conn.commit() 而不是 cursor.commit() 后遇到此错误的人,您可以使用enter_transaction_managementand leave_transaction_management(请注意,这是针对 Django 1.4.6 和 MySQL-python 1.2.5;我可能一旦我们完成 Django 升级,就必须更新它):

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