getWritableDatabase与getReadableDatabase的用法区别

这是getWritableDatabase的API文档说明
SQLiteDatabase android.database.sqlite. SQLiteOpenHelper.getWritableDatabase()
public SQLiteDatabase getWritableDatabase ()
Added in API level 1

Create and/or open a database that will be used for reading and writing. The first time this is called, the database will be opened and onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and/or onOpen(SQLiteDatabase) will be called.

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.) Errors such as bad permissions or a full disk may cause this method to fail, but future attempts may succeed if the problem is fixed.

Database upgrade may take a long time, you should not call this method from the application main thread, including from ContentProvider.onCreate().

Returns
  • a read/write database object valid until close() is called
Throws
SQLiteException

if the database cannot be opened for writing

表面上看,getWritableDatabase应该是取得一个用于写数据库的SQLiteDatabase实例,getReadableDatabase是取得一个用于读数据库的SQLiteDatabase实例

从APIW文档上解读

getWritableDatabase取得的实例不是仅仅具有写的功能,而是同时具有读和写的功能

同样的,getReadableDatabase取得的实例也是具对数据库进行读和写的功能

两者的区别在于

getWritableDatabase取得的实例是以读写的方式打开数据库,如果打开的数据库磁盘满了,此时只能读不能写,此时调用了getWritableDatabase的实例,那么将会发生错误(异常)

getReadableDatabase取得的实例是先调用getWritableDatabase以读写的方式打开数据库,如果数据库的磁盘满了,此时返回打开失败,继而用getReadableDatabase的实例以只读的方式去打开数据库

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值