Python 属性错误:';MySQLCursor';对象没有属性';提交';
我得到以下错误:Python 属性错误:';MySQLCursor';对象没有属性';提交';,python,mysql,Python,Mysql,我得到以下错误: 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.
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()
它是怎么来的,哪里出了问题?
我尝试连接MySQLWorkbench
编辑:
现在我得到以下错误:
AttributeError: 'MySQLCursor' object has no attribute 'commit'
因为你不能提交游标!您必须提交连接
mysql.connector.errors.DatabaseError: 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
…在修复一些遗留代码(显然已经有几年没有工作了,所以用户不再尝试使用它)时,我们遇到了相同的错误,使用Django中的MySQL python包。但是,由于Django ORM中出现了不同的错误,因此使用关于此答案和其他答案的建议会导致不同的错误: django.db.transaction.TransactionManagementError:此代码不是 在交易管理下 因此,对于那些在使用conn.commit()而不是cursor.commit()后遇到此错误的人,您可以使用
enter\u transaction\u management
和leave\u transaction\u management
(请注意,这是针对Django 1.4.6和MySQL python 1.2.5的;一旦Django升级完成,我可能必须更新此项):
哦,我使用了conn.close()而不是cursor.close()你可以随时编辑你的帖子你必须使用
conn.commit()
而不是cursor.commit()
现在我得到了以下错误:“mysql.connector.errors.DatabaseError:1205(HY000):超过了锁定等待超时;尝试重新启动事务”检查有关数据库超时的信息。可能您的titel_lijst
太长,插入的行太多,占用了很多时间,超过了MySql事务超时限制,因此您的连接超时。我使用的是,connection.commit(),但得到的是相同的错误
# cursor.commit() --> This is wrong!
conn.commit() # This is right
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()