可能有人解释这行做了什么? [英] Could someone explain what this line does?
问题描述
在本博客:
的http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
这是一个行 this.getReadableDatabase();
,我不明白它做什么,但如果我从我的code将停止工作删除
/ **
*在系统上创建一个空数据库,并与自己的数据库重写它。
* * /
公共无效的CreateDatabase()抛出IOException 布尔dbExist = checkDataBase(); 如果(dbExist){
//什么也不做 - 已存在于数据库
}其他{ //通过调用此方法与空的数据库将被创建到默认的系统路径
//你的应用程序,所以我们要能够覆盖该数据库与我们的数据库。
this.getReadableDatabase(); 尝试{ copyDataBase(); }赶上(IOException异常五){ 抛出新的错误(错误复制数据库); }
}}
从功能实现,你可以看到,这个API调用getWritableDatabase打开数据库()。在情况下,它由于某种原因,它打开在只读模式分贝失败。
公共同步SQLiteDatabase getReadableDatabase(){
如果(mDatabase =空&放大器;!&放大器; mDatabase.isOpen()){
返回mDatabase; //数据库已开门营业
} 如果(mIsInitializing){
抛出新IllegalStateException异常(getReadableDatabase递归调用);
} 尝试{
返回getWritableDatabase();
}赶上(SQLiteException E){
如果(MNAME == NULL)抛出Ë; //无法打开临时数据库的只读!
Log.e(TAG,无法打开+ MNAME +写(会尝试只读),E);
} SQLiteDatabase DB = NULL;
尝试{
mIsInitializing = TRUE;
字符串路径= mContext.getDatabasePath(MNAME).getPath();
DB = SQLiteDatabase.openDatabase(路径,mFactory,SQLiteDatabase.OPEN_READONLY);
如果(db.getVersion()!= mNewVersion){
抛出新SQLiteException(无法从版本升级只读数据库+
db.getVersion()+至+ mNewVersion +:+路径);
} 的OnOpen(DB);
Log.w(TAG,在只读模式,开业+ MNAME +);
mDatabase = DB;
返回mDatabase;
} {最后
mIsInitializing = FALSE;
如果(DB = NULL&放大器;!&安培;!DB = mDatabase)db.close();
}
}
下面是getWritableDatabase的实现()
公共同步SQLiteDatabase getWritableDatabase(){
如果(mDatabase =空&放大器;!&放大器; mDatabase.isOpen()及&放大器;!mDatabase.isReadOnly()){
返回mDatabase; //数据库已开门营业
} 如果(mIsInitializing){
抛出新IllegalStateException异常(getWritableDatabase递归调用);
} //如果我们有一个只读数据库打开,有人可能会使用它
//(虽然他们不应该),这将导致举行的锁
//该文件,我们试图打开数据库的读写会
//失败等待文件锁。以prevent的是,我们获得了
//只读数据库,它关闭了其他用户的锁。 布尔成功= FALSE;
SQLiteDatabase DB = NULL;
如果(!mDatabase = NULL)mDatabase.lock();
尝试{
mIsInitializing = TRUE;
如果(MNAME == NULL){
DB = SQLiteDatabase.create(NULL);
}其他{
DB = mContext.openOrCreateDatabase(MNAME,0,mFactory);
} INT版本= db.getVersion();
如果(版本!= mNewVersion){
db.beginTransaction();
尝试{
如果(版本== 0){
的onCreate(DB);
}其他{
onUpgrade(DB,版本,mNewVersion);
}
db.setVersion(mNewVersion);
db.setTransactionSuccessful();
} {最后
db.endTransaction();
}
} 的OnOpen(DB);
成功= TRUE;
返回分贝;
} {最后
mIsInitializing = FALSE;
如果(成功){
如果(mDatabase!= NULL){
尝试{mDatabase.close(); }赶上(例外五){}
mDatabase.unlock();
}
mDatabase = DB;
}其他{
如果(!mDatabase = NULL)mDatabase.unlock();
如果(DB!= NULL)db.close();
}
}
}
In this blog: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
It is one line this.getReadableDatabase();
, I don't understand what it does, but if I remove it from my code it stops working.
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
From the function implementation you can see that this api opens the database by calling getWritableDatabase(). In case it fails due to some reason, it opens the db in a read-only mode.
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();
}
}
Here is the implementation of getWritableDatabase()
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();
}
}
}
这篇关于可能有人解释这行做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!