clearAllTables 不起作用

clearAllTables doesn't work

Android Room 有方法 void clearAllTables() 并且根据文档,它执行以下操作:

Deletes all rows from all the tables that are registered to this database as entities().

This does NOT reset the auto-increment value generated by autoGenerate().

After deleting the rows, Room will set a WAL checkpoint and run VACUUM. This means that the data is completely erased. The space will be reclaimed by the system if the amount surpasses the threshold of database file size.

我在我的项目中检查了它,看起来 db 在那个调用之后没有数据,但是当我拉 *.db 文件并在 sqlite 查看器中打开我看到所有数据都存在,表格已填满,没有任何内容被删除。 怎么可能?我认为这是我的应用程序中的一个潜在缺陷。请提供可靠的清理房间数据库的方法

looks like db has no data after that call

说明方法有效


when I pulled *.db file from my device and opened it in sqlite viewer I've seen that all the data exists

很可能交易还没有从 WAL 文件移动到原始数据库。


解决方案

您可以使用 wal_checkpoint 编译指示强制检查点。针对数据库查询以下语句。

pragma wal_checkpoint(full)