2012-04-28 8 views
2

私はこの権利を得ようとしていますが、私は何が欠けているのか分かりません。私はアンドロイドアプリを持っており、それに1つのテーブルを追加したいと思っています。私はそれを行うことができませんし、例外もありません(これらのサイレントキラーがいらない!!)。アップグレードテーブルを作成

は以下

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

    } 

} 

最悪の部分は、私はすべてのログが起こって見ると、私は、コンソール上でそれを行う場合でも、クエリが正常に実行されている私のSQLiteHelperクラス私のコードです。


編集

私のDBが更新されていません: -

私はエミュレータからの私のDBを引っ張ってきたとさえバージョンからのログがでライン以下changed.I使用しないno.is見私の活動からDB版を見ることができます。

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

どのように機能していないと判断していますか? – Barak

+0

@Barak:logs.seeの編集を追加しました.. –

答えて

5

私は強く、全体のパッケージ・データベース・イン・アプリのパターンが出て働いていた、SQLiteAssetHelperに切り替えることをお勧めします。

戦術的には、取引をコミットしていません。複数文のトランザクションのための適切なレシピは次のとおりです。setTransactionSuccessful()コールなし

try { 
    db.beginTransaction(); 

    // do SQL here 

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

endTransaction()ROLLBACKを行います。

+0

いいですね...試してみます...ありがとうございます –

+0

はい、完了しました。ちょうどdb.setTransactionSuccessful()を試みましたが、SQLiteAssetHelperでも非常に素晴らしく簡単でした。私は考えていましたonUpgradeのチェックとしてアプリケーションバージョンを使用し、私のasset.soに新しいテーブルを持つDBを持つこの方法では、新しいユーザーも最新のテーブルを持っています。 –

+0

AssetHelperについて教えてください。 – Khan

関連する問題