开发者

Why was MyISAM a fix to this problem caused by InnoDB?

开发者 https://www.devze.com 2023-03-08 21:06 出处:网络
I was experiencing a very weird bug in my Python on MySQL app. The following code was in a loop that was called many times. The code never raised an error, but also never inserted any data:

I was experiencing a very weird bug in my Python on MySQL app. The following code was in a loop that was called many times. The code never raised an error, but also never inserted any data:

    cursor = db.cursor()
    cursor.execute("""
        INSERT INTO test_1 (value) VALUES (10000);
    """)

Another strange symptom was that inserting data into the same table using phpMyAdmin later on produced very large auto_increment IDs, so the auto_increment counter was being incremented even though no data was actually in the table (according to SELECT * FROM test_1 anyway). Insertions worked fine using MySQL Workbe开发者_如何学编程nch.

In the end I randomly discovered that using the MyISAM engine on the table instead of InnoDB fixed it. Why?


Most probably because you never called commit. MySQLdb (I suppose that's the module you're using) automatically starts a transaction for every connection you make to your database server. All the insert queries work just fine and increment the auto_incrementcounter, but you never see the results from phpMyAdmin because InnoDB isolates its connection from the changes made via python. If your Python script disconnects and no commit call was made, the transaction is rolled back and no changes (apart from the incremented auto_increment counter) are persisted. See if a db.commit() (assuming db is the connection object) helps.

Also see PEP 249 on database access in Python.

0

精彩评论

暂无评论...
验证码 换一张
取 消