简体   繁体   English

Android中的SQLite是否有内存缓存以及如何释放或清除它?

[英]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: 首先,我在Android应用中创建了一个名为“mydb”的数据库:

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). 但是,我手动删除这个数据库不是通过编程,使用软件“RE探索”(当然在root设备上)。

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: 引自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.) (确保在不再需要数据库时调用close()。)

This is from the description of the getWritableDatabase() method, however both getReadableDatabase() and getWritableDatabase() return basically the same object for reading the database. 这是来自getWritableDatabase()方法的描述,但getReadableDatabase()getWritableDatabase()返回基本相同的对象来读取数据库。

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. 请注意,如果要将对数据库所做的更改保留在设备的内部存储器中,则应使用getWritableDatabase() 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. 如果要完全删除数据库,则应调用SQLiteDatabase的close()方法以使缓存无效。

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

use SQLiteDatabase.deleteDatabase(File file) API to delete the database 使用SQLiteDatabase.deleteDatabase(File file) API删除数据库

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 如果您无法做到这一点,只需调用deleteDatabase,然后执行kill process .. - 不推荐

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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