2012-04-28 35 views
2

我一直試圖讓這個權利,但我不知道我錯過了什麼。我有一個Android應用程序,我希望再添加1個表格。但是,我無法做到這一點,我也沒有例外(不喜歡這些無聲的殺手!!)。在升級時創建表格

下面是我的代碼我SQLiteHelper類

public class DbCreator extends SQLiteOpenHelper { 

public DbCreator(Context context) { 

    super(context, Constants.DB_NAME, null, Constants.NEW_VERSION);//NEW_VERSION=2 

    this.myContext = context; 
} 

//Rest of code 

//Checks if DB is present and create if reqd. 
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 : " + e); 

     } 
    } 

} 

    //Check DB is present 
    private boolean checkDataBase() { 

    SQLiteDatabase checkDB = null; 

    try { 
     String myPath = Constants.DB_PATH + Constants.DB_NAME; 
     checkDB = SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.OPEN_READONLY); 

    } catch (SQLiteException e) { 
     Log.v("DB", "No DB"); 
     // database does't exist yet. 

    } 

    if (checkDB != null) { 

     checkDB.close(); 

    } 

    return checkDB != null ? true : false; 
} 

/** 
* Copies your database from local assets-folder to the just created 
* empty database in the system folder, from where it can be accessed and 
* handled. This is done by transfering bytestream. 
* */ 
private void copyDataBase() throws IOException { 

    // Open your local db as the input stream 
    InputStream myInput = myContext.getResources().openRawResource(
      R.raw.diary_database); 

    // Path to the just created empty db 
    String outFileName = Constants.DB_PATH + Constants.DB_NAME; 

    // Open the empty db as the output stream 
    OutputStream myOutput = new FileOutputStream(outFileName); 

    // transfer bytes from the inputfile to the outputfile 
    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = myInput.read(buffer)) > 0) { 
     myOutput.write(buffer, 0, length); 
    } 

    // Close the streams 
    myOutput.flush(); 
    myOutput.close(); 
    myInput.close(); 

} 

    //**This is the problem area 
    @Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    Constants.log(TAG, "Upgrading started..."+db.getVersion()); 
    if (db.getVersion() == Constants.OLD_VERSION) { 
      db.beginTransaction(); 
     Constants.log(TAG, "Upgrading started...transaction"); 
     db.execSQL("create TABLE Thought( StartDate DATETIME, Content TEXT, EndDate DATETIME, Title TEXT)"); 
     db.setVersion(Constants.NEW_VERSION);//NEW_VERSION=2 
      db.endTransaction(); 
     Constants.log(TAG, "Upgrading started...transaction finished"); 

    } 

} 

最糟糕的是,我看到所有的日誌事件,當我做它的控制檯連查詢成功運行。


編輯

我的數據庫不被更新: -

我已經退出從模擬器我的數據庫,甚至看到日誌從版本no.is以下線不changed.I使用我的活動從看到DB版本。

DbCreator dbCr = new DbCreator(this); 
SQLiteDatabase myDataBase = dbCr.getMyDatabase(); 
Constants.log(TAG, "Db Version : "+myDataBase.getVersion()); 
+0

你怎麼確定它沒有工作? – Barak 2012-04-28 14:32:18

+0

@Barak:已添加logs.see編輯.. – 2012-04-28 16:41:17

回答

5

我強烈建議您切換到SQLiteAssetHelper,其中有全包的數據庫,在最應用模式制定。

在戰術上,您並未提交交易。正確的配方多語句事務是:

try { 
    db.beginTransaction(); 

    // do SQL here 

    db.setTransactionSuccessful(); 
} 
finally { 
    db.endTransaction(); 
} 

沒有setTransactionSuccessful()電話,endTransaction()會做一個ROLLBACK

+0

看起來不錯...將嘗試...謝謝 – 2012-04-28 16:41:59

+0

是的它得到完成。只是嘗試了db.setTransactionSuccessful(),但即使SQLiteAssetHelper似乎非常好,easy.I在想在onUpgrade中使用應用程序版本進行檢查,並在資產中使用新表的數據庫。這樣,新用戶將擁有最新的表格。 – 2012-04-28 17:45:32

+0

有關AssetHelper的相關信息。 – Khan 2012-04-30 07:59:57