Android使用getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例。(getReadableDatabase()方法中会调用getWritableDatabase()方法)

其中getWritableDatabase() 方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,倘若使用的是getWritableDatabase() 方法就会出错。

getReadableDatabase()方法则是先以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库。如果该问题成功解决,则只读数据库对象就会关闭,然后返回一个可读写的数据库对象。

源码如下:

  1. /**
  2. * Create and/or open a database that will be used for reading and writing.
  3. * Once opened successfully, the database is cached, so you can call this
  4. * method every time you need to write to the database.  Make sure to call
  5. * {@link #close} when you no longer need it.
  6. *
  7. * <p>Errors such as bad permissions or a full disk may cause this operation
  8. * to fail, but future attempts may succeed if the problem is fixed.</p>
  9. *
  10. * @throws SQLiteException if the database cannot be opened for writing
  11. * @return a read/write database object valid until {@link #close} is called
  12. */
  13. public synchronized SQLiteDatabase getWritableDatabase() {
  14. if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
  15. return mDatabase;  // The database is already open for business
  16. }
  17. if (mIsInitializing) {
  18. throw new IllegalStateException("getWritableDatabase called recursively");
  19. }
  20. // If we have a read-only database open, someone could be using it
  21. // (though they shouldn't), which would cause a lock to be held on
  22. // the file, and our attempts to open the database read-write would
  23. // fail waiting for the file lock.  To prevent that, we acquire the
  24. // lock on the read-only database, which shuts out other users.
  25. boolean success = false;
  26. SQLiteDatabase db = null;
  27. if (mDatabase != null) mDatabase.lock();
  28. try {
  29. mIsInitializing = true;
  30. if (mName == null) {
  31. db = SQLiteDatabase.create(null);
  32. } else {
  33. db = mContext.openOrCreateDatabase(mName, 0, mFactory);
  34. }
  35. int version = db.getVersion();
  36. if (version != mNewVersion) {
  37. db.beginTransaction();
  38. try {
  39. if (version == 0) {
  40. onCreate(db);
  41. } else {
  42. onUpgrade(db, version, mNewVersion);
  43. }
  44. db.setVersion(mNewVersion);
  45. db.setTransactionSuccessful();
  46. } finally {
  47. db.endTransaction();
  48. }
  49. }
  50. onOpen(db);
  51. success = true;
  52. return db;
  53. } finally {
  54. mIsInitializing = false;
  55. if (success) {
  56. if (mDatabase != null) {
  57. try { mDatabase.close(); } catch (Exception e) { }
  58. mDatabase.unlock();
  59. }
  60. mDatabase = db;
  61. } else {
  62. if (mDatabase != null) mDatabase.unlock();
  63. if (db != null) db.close();
  64. }
  65. }
  66. }
  67. /**
  68. * Create and/or open a database.  This will be the same object returned by
  69. * {@link #getWritableDatabase} unless some problem, such as a full disk,
  70. * requires the database to be opened read-only.  In that case, a read-only
  71. * database object will be returned.  If the problem is fixed, a future call
  72. * to {@link #getWritableDatabase} may succeed, in which case the read-only
  73. * database object will be closed and the read/write object will be returned
  74. * in the future.
  75. *
  76. * @throws SQLiteException if the database cannot be opened
  77. * @return a database object valid until {@link #getWritableDatabase}
  78. *     or {@link #close} is called.
  79. */
  80. public synchronized SQLiteDatabase getReadableDatabase() {
  81. if (mDatabase != null && mDatabase.isOpen()) {
  82. return mDatabase;  // The database is already open for business
  83. }
  84. if (mIsInitializing) {
  85. throw new IllegalStateException("getReadableDatabase called recursively");
  86. }
  87. try {
  88. return getWritableDatabase();
  89. } catch (SQLiteException e) {
  90. if (mName == null) throw e;  // Can't open a temp database read-only!
  91. Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
  92. }
  93. SQLiteDatabase db = null;
  94. try {
  95. mIsInitializing = true;
  96. String path = mContext.getDatabasePath(mName).getPath();
  97. db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);
  98. if (db.getVersion() != mNewVersion) {
  99. throw new SQLiteException("Can't upgrade read-only database from version " +
  100. db.getVersion() + " to " + mNewVersion + ": " + path);
  101. }
  102. onOpen(db);
  103. Log.w(TAG, "Opened " + mName + " in read-only mode");
  104. mDatabase = db;
  105. return mDatabase;
  106. } finally {
  107. mIsInitializing = false;
  108. if (db != null && db != mDatabase) db.close();
  109. }
  110. }

Android getReadableDatabase() 和 getWritableDatabase()的更多相关文章

  1. getReadableDatabase 和 getWritableDatabase的区别

    (1)getWritableDatabase()方法以读写方式打开数据库.一旦数据库的磁盘空间满了,数据库就只能读而不能写,此时用getWritableDatabase()打开数据库就会出错. (2) ...

  2. Android之SqlLite数据库使用

    每个应用程序都要使用数据,Android应用程序也不例外,Android使用开源的.与操作系统无关的SQL数据库—SQLite.SQLite第一个Alpha版本诞生于2000年5月,它是一款轻量级数据 ...

  3. 疯狂Android讲义 - 学习笔记(七)

    第8章 Android数据存储与IO  Java IO的数据存储可以移植到Android应用开发上来,Android系统还提供了一些专门的IO API. Android系统内置了SQLite数据库,S ...

  4. Android中的数据保存

    形式 Android的数据保存分为3种形式:file, SharedPreference, Database 文件 主要思想就是通过Context类中提供的openFileInput和openFile ...

  5. Android SQLiteOpenHelper类的使用

    SQLiteOpenHelper类是Android平台提供的用于SQLite数据库的创建.打开以及版本管理的帮助类.一般需要继承并这个类并实现它的onCreate和onUpgrade方法,在构造方法中 ...

  6. Android入门(十)SQLite创建升级数据库

    原文链接:http://www.orlion.ga/603/ 一.创建数据库 Android为了让我们能够更加方便地管理数据库,专门提供了一个 SQLiteOpenHelper帮助类, 借助这个类就可 ...

  7. Android入门(十一)SQLite CURD

    原文链接:http://www.orlion.ga/594/ 一.添加数据 SQLiteOpenHelper的getReadableDatabase()或getWritableDatabase()方法 ...

  8. 67.Android中的数据存储总结

    转载:http://mp.weixin.qq.com/s?__biz=MzIzMjE1Njg4Mw==&mid=2650117688&idx=1&sn=d6c73f9f04d0 ...

  9. Android本地数据存储之SQLite关系型数据库 ——SQLiteDatabase

    数据库的创建,获取,执行sql语句: 框架搭建:dao 思考: 1.数据库保存在哪里? 2.如何创建数据库?如何创建表? 3.如何更新数据库?如何更改表的列数据? 4.如何获取数据库? 5.如何修改数 ...

随机推荐

  1. android-Java SoftReference,WeakReference,Direct Reference简介

    主要部分: SoftReference(软引用)是java中一个用来实现缓存内容的类.通过此类,可以观察某对象什么时候会被垃圾收集的执行绪清除.被 Soft Reference 指到的对象,即使没有任 ...

  2. grep 基于关键字搜索

    grep 'linux' /etc/passwd 搜索passwd文件下的包含linux的行 find / -user linux|grep Video 在用户为linux的根目录下搜房Video内容 ...

  3. 仍需"敬请期待"的微信沃卡

           从2013年7月30日广东联通联合腾讯公布将合作推出联通沃卡,到8月5日在易迅网上进行预订,8月8日正式发售,再到本人最近几日拿到预订的实卡,已经过去20多天了.于是乎,我怀着无比期待的 ...

  4. Apache的Access.log分析总结

    Apache的Access.log分析总结 #查看80端口的tcp连接  #netstat -tan | grep "ESTABLISHED" | grep ":80&q ...

  5. Hadoop-Yarn-框架原理及运作机制(原理篇)

    文件为转载:http://blog.csdn.net/liuwenbo0920/article/details/43304243 一.YARN基本架构 YARN是Hadoop 2.0中的资源管理系统, ...

  6. html系列教程--p param progress rp rt ruby script select small source

    <p> 标签:用户段落划分或折行的标签 <param> 标签:param 元素允许您为插入 XHTML 文档的对象规定 run-time 设置,也就是说,此标签可为包含它的 & ...

  7. HTTP协议探析

    1.HTTP协议概述 超文本传输协议(HTTP)是一种为分布式,协作式的,超媒体信息系统.它是一种通用的,无状态(stateless)的协议,除了应用于超文本传输外,它也可以应用于诸如名称服务器和分布 ...

  8. 对List对象按照某个成员变量进行排序

    /** * 对List对象按照某个成员变量进行排序 * @param list List对象 * @param sortField 排序的属性名称 * @param sortMode 排序方式:ASC ...

  9. 安装jdk和tomcat

    安装jdk和tomcat 1,准备工作 虚拟机 VMware :liunx系统镜像 bebian :连接操作软件 putty: 开源图像FTP客户端winspc: Java 语言的软件开发工具包 JD ...

  10. DevExpress中GridControl的属性设置

    1.隐藏最上面的GroupPanel gridView1.OptionsView.ShowGroupPanel=false; 2.得到当前选定记录某字段的值 sValue=Table.Rows[gri ...