如何使用 truncate 在 mysqldb 中执行事务

How to do a transaction in mysqldb with truncate

我正在尝试截断一个 table 然后做一些插入,然后在最后全部提交。但是,执行以下操作会立即清除 table:

>>> t.cursor.execute('START TRANSACTION;')
0L
>>> t.cursor.execute('TRUNCATE _tx;')
0L
# table is now cleared from truncate

如何将 'truncate' 延迟到提交交易之后?

您不能在事务中使用 TRUNCATE,因为它会自动提交。最好这样做:

>>> t.cursor.execute('DELETE FROM _tx;')