简体   繁体   中英

Is there a memory cache for SQLite in Android and how to release or clear it?

Firstly, I create a database called "mydb" in my Android app:

DBHelper dbHelper = new DBHelper(context, "mydb", null, 1);//DBHelper is my custom class

And write some data into it's table:

SQLiteDatabase db = dbHelper.getReadableDatabase();
db.execSQL("insert into mytable(name, text) values ('allen','hello')");

Here, everything is ok. But then, i delete this database manually not by programming, with a software "RE explore" (Certainly on a rooted device).

Then, in my code, i read this table of the database. What is astonishing is that i still could get the data I stored.

Cursor cursor = db.query("mytable", new String[]{"name","text"}, null, null, null, null, null);

Why?

Quoting from the Android Developers reference website:

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.)

This is from the description of the getWritableDatabase() method, however both getReadableDatabase() and getWritableDatabase() return basically the same object for reading the database.

Please note that you should use getWritableDatabase() if you want to persist the changes you make to the database on the device's internal memory. Otherwise they will be valid only for the duration of the application's runtime and will be discarded once the app is closed. If you wish to delete the database completely, you should call the SQLiteDatabase's close() method in order to invalidate the cache.

您需要从手机中删除该应用,然后重新安装

use SQLiteDatabase.deleteDatabase(File file) API to delete the database

Deletes a database including its journal file and other auxiliary files that may have been created by the database engine.

Make sure you have closed all the connections that are open.

In case you are not able to do that, just cal the deleteDatabase followed by kill process.. - not recommended

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM