Android getReadableDatabase() 和 getWritableDatabase()分析对比
程序员文章站
2023-11-16 20:54:46
android getreadabledatabase() 和 getwritabledatabase()分析对比
android使用getwritabledatabas...
android getreadabledatabase() 和 getwritabledatabase()分析对比
android使用getwritabledatabase()和getreadabledatabase()方法都可以获取一个用于操作数据库的sqlitedatabase实例。(getreadabledatabase()方法中会调用getwritabledatabase()方法)
其中getwritabledatabase() 方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,倘若使用的是getwritabledatabase() 方法就会出错。
getreadabledatabase()方法则是先以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库。如果该问题成功解决,则只读数据库对象就会关闭,然后返回一个可读写的数据库对象。
源码如下:
/** * create and/or open a database that will be used for reading and writing. * 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 * {@link #close} when you no longer need it. * * <p>errors such as bad permissions or a full disk may cause this operation * to fail, but future attempts may succeed if the problem is fixed.</p> * * @throws sqliteexception if the database cannot be opened for writing * @return a read/write database object valid until {@link #close} is called */ public synchronized sqlitedatabase getwritabledatabase() { if (mdatabase != null && mdatabase.isopen() && !mdatabase.isreadonly()) { return mdatabase; // the database is already open for business } if (misinitializing) { throw new illegalstateexception("getwritabledatabase called recursively"); } // if we have a read-only database open, someone could be using it // (though they shouldn't), which would cause a lock to be held on // the file, and our attempts to open the database read-write would // fail waiting for the file lock. to prevent that, we acquire the // lock on the read-only database, which shuts out other users. boolean success = false; sqlitedatabase db = null; if (mdatabase != null) mdatabase.lock(); try { misinitializing = true; if (mname == null) { db = sqlitedatabase.create(null); } else { db = mcontext.openorcreatedatabase(mname, 0, mfactory); } int version = db.getversion(); if (version != mnewversion) { db.begintransaction(); try { if (version == 0) { oncreate(db); } else { onupgrade(db, version, mnewversion); } db.setversion(mnewversion); db.settransactionsuccessful(); } finally { db.endtransaction(); } } onopen(db); success = true; return db; } finally { misinitializing = false; if (success) { if (mdatabase != null) { try { mdatabase.close(); } catch (exception e) { } mdatabase.unlock(); } mdatabase = db; } else { if (mdatabase != null) mdatabase.unlock(); if (db != null) db.close(); } } } /** * create and/or open a database. this will be the same object returned by * {@link #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 {@link #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. * * @throws sqliteexception if the database cannot be opened * @return a database object valid until {@link #getwritabledatabase} * or {@link #close} is called. */ public synchronized sqlitedatabase getreadabledatabase() { if (mdatabase != null && mdatabase.isopen()) { return mdatabase; // the database is already open for business } if (misinitializing) { throw new illegalstateexception("getreadabledatabase called recursively"); } try { return getwritabledatabase(); } catch (sqliteexception e) { if (mname == null) throw e; // can't open a temp database read-only! log.e(tag, "couldn't open " + mname + " for writing (will try read-only):", e); } sqlitedatabase db = null; try { misinitializing = true; string path = mcontext.getdatabasepath(mname).getpath(); db = sqlitedatabase.opendatabase(path, mfactory, sqlitedatabase.open_readonly); if (db.getversion() != mnewversion) { throw new sqliteexception("can't upgrade read-only database from version " + db.getversion() + " to " + mnewversion + ": " + path); } onopen(db); log.w(tag, "opened " + mname + " in read-only mode"); mdatabase = db; return mdatabase; } finally { misinitializing = false; if (db != null && db != mdatabase) db.close(); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
推荐阅读
-
Android getReadableDatabase() 和 getWritableDatabase()分析对比
-
三星Note Pro和Tab Pro区别对比分析
-
php和C#的yield迭代器实现方法对比分析
-
企业做营销型网站和做展示型网站的对比分析
-
对比分析MySQL语句中的IN 和Exists
-
奔腾G5400和R3 2200G哪个好 奔腾G5400和锐龙3 2200G详细对比分析
-
联发科P22和骁龙625哪个好 骁龙625与联发科Helio P22对比分析
-
荣耀盒子和创维盒子哪款更好用? 全面对比分析
-
python中in在list和dict中查找效率的对比分析
-
MacBook和13英寸MacBook Pro规格对比分析