Android getWritableDatabase() 和 getReadableDatabase() 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10844578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between getWritableDatabase() and getReadableDatabase()?
提问by Tai Tran
I use reading and writing to the database without any problems . But I can not find out the difference. I searched on Internet but it 's not quite clear. Can anyone tell me the difference? In which case should I use getWritableDatabase()
or getReadableDatabase()
?
我使用读取和写入数据库没有任何问题。但我找不到区别。我在互联网上搜索过,但不是很清楚。谁能告诉我有什么区别?在哪种情况下我应该使用getWritableDatabase()
或getReadableDatabase()
?
回答by Mark
Taken from my deleted answer here(this question is a duplicate of that one).
摘自我在此处删除的答案(这个问题是那个问题的重复)。
In normal situations,
getReadableDatabase()
will return the same writable database returned bygetWritableDatabase()
.However, should it not be possible to return a writable database,
getWritableDatabase()
will fail, whereasgetReadableDatabase()
will attempt to return aREAD_ONLY
database.
在正常情况下,
getReadableDatabase()
将返回与getWritableDatabase()
.但是,如果无法返回可写数据库,
getWritableDatabase()
则会失败,而getReadableDatabase()
会尝试返回READ_ONLY
数据库。
回答by Imran Rana
Check the Reference
检查参考
public synchronized SQLiteDatabase getReadableDatabase ()
公共同步SQLiteDatabase getReadableDatabase()
Since: API Level 1
从:API 级别 1
Create and/or open a database. This will be the same object returned by getWritableDatabase() unless some problem, such as a full disk, requires the database to be opened read-only. In that case, a read-only database object will be returned. If the problem is fixed, a future call to getWritableDatabase() may succeed, in which case the read-only database object will be closed and the read/write object will be returned in the future.
创建和/或打开数据库。这将与 getWritableDatabase() 返回的对象相同,除非某些问题(例如磁盘已满)要求以只读方式打开数据库。在这种情况下,将返回只读数据库对象。如果问题得到解决,未来对 getWritableDatabase() 的调用可能会成功,在这种情况下,只读数据库对象将被关闭,读/写对象将在未来返回。
Like getWritableDatabase(), this method may take a long time to return, so you should not call it from the application main thread, including from ContentProvider.onCreate(). Returns
与 getWritableDatabase() 一样,此方法可能需要很长时间才能返回,因此您不应从应用程序主线程调用它,包括从 ContentProvider.onCreate() 调用。退货
a database object valid until getWritableDatabase() or close() is called.
Throws SQLiteException if the database cannot be opened
如果无法打开数据库,则抛出 SQLiteException
public synchronized SQLiteDatabase getWritableDatabase ()
公共同步SQLiteDatabase getWritableDatabase()
Since: API Level 1
从:API 级别 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.
创建和/或打开将用于读取和写入的数据库。第一次调用时,将打开数据库并调用 onCreate(SQLiteDatabase)、onUpgrade(SQLiteDatabase, int, int) 和/或 onOpen(SQLiteDatabase)。
一旦打开成功,数据库就被缓存了,所以每次需要写数据库的时候都可以调用这个方法。(当您不再需要数据库时,请确保调用 close()。)错误权限或磁盘已满等错误可能会导致此方法失败,但如果问题得到解决,以后的尝试可能会成功。
Database upgrade may take a long time, you should not call this method from the application main thread, including from ContentProvider.onCreate(). Returns
数据库升级可能需要很长时间,您不应该从应用程序主线程调用此方法,包括从 ContentProvider.onCreate()。退货
a read/write database object valid until close() is called
Throws SQLiteException if the database cannot be opened for writing
如果无法打开数据库进行写入,则抛出 SQLiteException
回答by user1957229
The different between two is the condition when disk become full. Both the getReadableDatabase()
and getWritableDatabase()
task is to open/create database.
两者的区别在于磁盘变满时的情况。无论是getReadableDatabase()
和getWritableDatabase()
任务是打开/创建数据库。
But when the disk got full application crashes if you make a call to getWritableDatabase()
. However, getReadbleDatabase()
works fine in this case. Since, the getReadableDatabase()
blocks write operations and allows Read operation.
但是当磁盘已满时,如果您调用getWritableDatabase()
. 但是,getReadbleDatabase()
在这种情况下工作正常。因为,getReadableDatabase()
块写操作并允许读操作。
Don't call these methods from applications main thread like from onCreate()
method of MainActivity
in Android or any callback methods.
不要从应用这些方法为主线,从类似onCreate()
的方法MainActivity
在Android或任何回调方法。
回答by Dipankar Baghel
The main difference is -
主要区别是——
getReadbleDatabase() -
getReadbleDatabase() -
- Create and/or open a database. This will be the same object returned by getWritableDatabase() unless some problem, such as a full disk, requires the database to be opened read-only. In that case, a read-only database object will be returned. If the problem is fixed, a future call to getWritableDatabase() may succeed, in which case the read-only database object will be closed and the read/write object will be returned in the future.
- 创建和/或打开数据库。这将与 getWritableDatabase() 返回的对象相同,除非某些问题(例如磁盘已满)要求以只读方式打开数据库。在这种情况下,将返回只读数据库对象。如果问题得到解决,未来对 getWritableDatabase() 的调用可能会成功,在这种情况下,只读数据库对象将被关闭,读/写对象将在未来返回。
getWritableDatabase() -
getWritableDatabase() -
- 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.
- 创建和/或打开将用于读取和写入的数据库。第一次调用时,将打开数据库并调用 onCreate(SQLiteDatabase)、onUpgrade(SQLiteDatabase, int, int) 和/或 onOpen(SQLiteDatabase)。一旦打开成功,数据库就被缓存了,所以每次需要写数据库的时候都可以调用这个方法。(当您不再需要数据库时,请确保调用 close()。)错误权限或磁盘已满等错误可能会导致此方法失败,但如果问题得到解决,以后的尝试可能会成功。