2011-09-17 76 views
-2

在這個博客: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/有人可以解釋這條線是什麼嗎?

這是一條線this.getReadableDatabase();,我不明白它做什麼,但如果我從我的代碼中刪除它,它停止工作。

/** 
* 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"); 

     } 
    } 

} 
+3

有一個有用的評論只是上面。另外,你是否嘗試搜索任何文檔?你有沒有遇到http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getReadableDatabase()? –

+0

閱讀文檔! http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getReadableDatabase() –

回答

2

從功能的實現可以看到設置的生命週期,這個API通過打開數據庫調用getWritableDatabase()。如果由於某種原因失敗,它將以只讀模式打開數據庫。

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(); 
    } 
} 

這裏是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(); 
     } 
    } 
} 
+0

這與這個問題有什麼關係? –

+1

問題是'getReadableDatabase()'做了什麼?'。 – Reno

1

this.getReadableDatabase();調用該方法getReadableDatabase();從SQLiteOpenHelper類。這是可能的,因爲DataBaseHelper是SQLiteOpenHelper的一個子類。這是由這條線決定的。

public class DataBaseHelper extends SQLiteOpenHelper 
1

這個類是SQLiteOpenHelper的子類。從該方法的參考

採取:[1]

創建和/或打開將被用於讀取和寫入 數據庫。第一次調用時,將打開數據庫 和onCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase,int,int) 和/或onOpen(SQLiteDatabase)將被調用。

因此,它基本上只是準備SQLiteDatabase爲您和處理與更新等

[1] http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getReadableDatabase%28%29

相關問題