属性错误:'MySQLCursor'对象没有属性'commit'

2024-09-27 02:27:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我得到以下错误:

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

它是怎么来的,哪里出了问题? 我试着和MySQLWorkbench联系。

编辑:

现在我得到以下错误:

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

Tags: poscloseconnectormysqlconncursorcommitscore
2条回答

在修复一些遗留代码时(这显然已经几年没用了,所以用户不再尝试使用它),我们遇到了同样的错误,在Django中使用MySQL python包。但是,使用此答案和其他答案的建议会导致不同的错误,因为它发生在Django ORM中:

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

因此,对于那些在使用conn.commit()而不是cursor.commit()之后遇到此错误的用户,您可以使用enter_transaction_managementleave_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()

因为你不能提交光标!您必须提交连接。

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

Check the docs。。。

相关问题 更多 >

    热门问题