android – clearAllTables不起作用

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强制检查点.根据数据库查询以下语句.

pragma wal_checkpoint(full)
点赞