首先,我在我的 Android 应用程序中创建了一个名为“mydb”的数据库:
DBHelper dbHelper = new DBHelper(context, "mydb", null, 1);//DBHelper is my custom class
然后将一些数据写入它的表中:
SQLiteDatabase db = dbHelper.getReadableDatabase();
db.execSQL("insert into mytable(name, text) values ('allen','hello')");
在这里,一切正常。但是,我手动删除了这个数据库,而不是通过编程,使用软件“R.E. explore”(当然是在 Root设备上)。
然后,在我的代码中,我读取了数据库的这张表。令人惊讶的是,我仍然可以获取我存储的数据。
Cursor cursor = db.query("mytable", new String[]{"name","text"}, null, null, null, null, null);
为什么?
最佳答案
引自 Android 开发者引用网站:
Once opened successfully, the database is cached, so you can call this method every time you need to write to the database. (Make sure to call close() when you no longer need the database.)
这是来自 getWritableDatabase()
方法的描述,但是 getReadableDatabase()
和 getWritableDatabase()
基本上返回相同的对象用于读取数据库。
请注意,如果您想将对数据库所做的更改保存在设备的内存中,则应使用 getWritableDatabase()
。否则,它们将仅在应用程序运行期间有效,并在应用程序关闭后被丢弃。如果你想彻底删除数据库,你应该调用SQLiteDatabase的close()
方法来使缓存失效。
关于android - Android中是否有SQLite的内存缓存,如何释放或清除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27290966/