Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

sqlite

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 "R.E. 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?

like image 604
xu allen Avatar asked Dec 04 '14 09:12

xu allen


People also ask

Does SQLite cache in memory?

SQLite provides an in-memory cache which you size according to the maximum number of database pages that you want to hold in memory at any given time. Berkeley DB also provides an in-memory cache that performs the same function as SQLite.

Where is SQLite stored in Android?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases.

How delete all data from SQLite table in Android?

sqlite> DELETE FROM table_name; Following is the basic syntax of DROP TABLE. sqlite> DROP TABLE table_name; If you are using DELETE TABLE command to delete all the records, it is recommended to use VACUUM command to clear unused space.


1 Answers

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.

like image 77
mittelmania Avatar answered Sep 27 '22 23:09

mittelmania