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

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

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

源码如下:

[java] view plaincopy

 

  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的区别

    在Android中,通过getReadableDatabase与getWritableDatabase()都可以获得一个可以对数据库进行操作的实例.然后都可以对数据库进行增.删.查.改.,但是两者也存 ...

  2. getReadableDatabase() 和 getWritableDatabase()的区别

    2019独角兽企业重金招聘Python工程师标准>>> Android使用getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于 ...

  3. getReadableDatabase VS getWritableDatabase

    public synchronized SQLiteDatabase getReadableDatabase() {if (mDatabase != null && mDatabase ...

  4. Android存储方式之SQLite

    前言 SQLite数据库操作在Android开发中非常常用 今天我将带大家全面了解关于SQLite数据库的操作(增.删.查.改) 目录 1. SQLite数据库介绍 SQLite是Android内置的 ...

  5. Android数据存储的三种方式-SharedPrefrences,File,SQLite

    1,使用SharedPrefrences 用于简单少量的数据,数据的格式简单:都是普通的字符串,标量类型的值等,比如各种配置信息等等 SharedPrefrences与Editor简介: 创建Shar ...

  6. Android学习--持久化(三) SQLite LitePal

    SQLite & LitePal 自己做为一个iOS开发,看到安卓这一块的时候,那中浓烈的熟悉味道更加强烈,SQLite这种轻量级的关系型数据库的使用在移动端相差不多,iOS有FMDB,And ...

  7. Android中实现SQLite数据库CRUD操作的两种方式

    Android中实现SQLite数据库CRUD操作的两种方式 SQLite是一款轻量级的关系型数据库,具有运行速度.占用资源少的特点.通常只需要几百KB的内存就够了,因此特别适合在移动设备上使用.SQ ...

  8. Android学习之SQLite

    androidsqlite数据库buttonstringlayout 目录(?)[-] SQLite简介 SQLite使用简介 在DOS环境对SQLite数据库的操作步骤 1.SQLite简介: SQ ...

  9. Android数据存储之SQLite

    概览 l  概述 l   CRUD方法详解 l  注意事项   概述 对于大量数据的处理,如果不想将数据存于服务器端,Android API提供了对关系数据库SQLite的支持,在android-SD ...

最新文章

  1. mxnet makeloss
  2. codeforces——Little Pony and Expected Maximum
  3. python手势识别_Python|使用opencv进行简单的手势检测
  4. matlabapp窗口图像_matlab – 如何自定义App Designer图形的背景?
  5. PCL “(”:“::”右边的非法标记 和 E2512 功能测试宏的参数必须是简单标识符
  6. python全局变量的声明和使用_python自学篇(第三章:函数)
  7. node实现基于token的身份验证
  8. 浅谈UWB室内定位(二)
  9. 使用octave符号运算求解不定积分、微分方程等(兼容matlab)
  10. 圈圈教你玩usb第一版件电子资源使用说明
  11. win10下如何解决VC++MSDEV.EXE的0xc0000142错误
  12. 微星主板Ubuntu16.04安装教程
  13. H3C-H3CNE 华三网络工程师从入门到精通 自学视频课程[肖哥]-肖宗鹏-专题视频课程...
  14. 计算机考研专业课408什么意思,考研408是什么意思
  15. Python面向对象加强2.Python 中类的内置属性和内置方法(魔法函数)
  16. oracle插入新字段脚本
  17. 用python祝男朋友生日快乐_祝男朋友生日快乐的说说50句
  18. Linux常用指令(5)——20.4.25
  19. Vue用图片制作Wifi动态图 制作小喇叭效果
  20. 聪明好学的王强用计算机设计了,五年级语文下册期中试卷-(1)(1).doc

热门文章

  1. abaqus python 读取文件_通过Python脚本从Abaqus中的excel文件导入幅度数据
  2. android 焦点动画,在一个视图/imageview上获得焦点时,实现android缩放动画?_animation_开发99编程知识库...
  3. 柱形图怎么变成横着的_鞋柜爆满怎么办?来看看日本人怎么收纳鞋子,感觉能多塞下20双...
  4. xp系统qq安装不上网络连接服务器,windows xp系统不能登录qq的解决方法
  5. 在的微型计算机系统中 外设可和,微机原理第七章题库
  6. python如何查询文件路径_Python使用os.listdir和os.walk获取文件路径
  7. win10 安装vue运行环境
  8. 利用Python进行数据分析--时间序列
  9. 手机浏览器网址_你真的会用浏览器搜索吗?几个高阶搜索技巧助您高效找到有用信息
  10. 简单完整的Python小爬虫教程