首先,我在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()方法以使缓存无效.