SQLiteOpenHelper.getWritableDatabase的用法 [英] Usage of SQLiteOpenHelper.getWritableDatabase

查看:258
本文介绍了SQLiteOpenHelper.getWritableDatabase的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据<一个href=\"http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getWritableDatabase%28%29\" rel=\"nofollow\">http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getWritableDatabase%28%29

在成功打开,数据库缓存,所以你可以调用这个
  方法每次需要写入数据库。

Once opened successfully, the database is cached, so you can call this method every time you need to write to the database.

考虑下面的代码片段:

SQLiteDatabase db1 = openHelperImplObj.getWritableDatabase();
// do something with db1
SQLiteDatabase db2 = openHelperImplObj.getWritableDatabase();
// do something with db2
db2.close();
// do something with db1
db1.close();

我presume到getWritableDatabase在第二个电话()将返回相同的缓存DB(在 DB1 ),岂不行db2.close()造成任何问题的发生后DB1操作?若然,是为了避免这样的问题,因为第二呼叫可以在不同的功能或甚至在不同类的任何地方发生,即,推荐的方法

I presume the second call to getWritableDatabase() will return the same cached DB (the db1), wouldn't the line db2.close() cause any issue to db1 operations that happen afterward? If so, what is the recommended way to avoid such problem since the second call may happen anywhere, i.e. in a different function or even in different class.

推荐答案

当您使用DB打开数据库 openHelperImplObj.getWritableDatabase();
然后下面的事情检查

When you open a data base by using db openHelperImplObj.getWritableDatabase(); then following thing check

1方式&gt; 如果未创建的数据库中创建它的数据库并打开它以其他写模式首先创建然后打开

1>If database is not created then it create database and open it for write mode else created first then open.

2 - ; 其次,最重要的,如果已经创建的数据库,然后它打开了它,但我所观察到的,如果它已经打开它什么都不做,这意味着它占据数据的旧实例
底座可打开的数据库已直到关闭it.Thats为什么它更好的做法,关闭数据库,一旦它
使用过

2>Second and most important what i have observed if data base is already created then it opened it but if its open already it do nothing, That means it occupy the old instance of data base that open DB already until you close it.Thats why its better practice to close DB once it use over

希望你有

这篇关于SQLiteOpenHelper.getWritableDatabase的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆