2013-01-09 93 views
3

我有一個應用程序v1.0,它使用保存在資產文件夾中的本地數據庫...我已將應用程序更新到版本1.1,在新版本中內部數據庫有不同的條目。當我更新應用程序時,資產文件夾文件不會更新

不幸的是,安裝的應用程序繼續使用版本1.0的數據庫,我必須卸載應用程序並再次安裝以使用最新版本的數據庫。

真的是一件壞事迫使買家撤銷應用程序並重新安裝新版本...爲什麼資產文件沒有用新版本更新?

編輯

我明白,這個問題發生,因爲數據庫從資產文件中創建且僅當尚未創建,這是我DatabaseHelper類

public class DataBaseHelper extends SQLiteOpenHelper { 
    private static String TAG = "DataBaseHelper"; 
    private static String DB_PATH = ""; 
    private static String DB_NAME = "MyDatabaseFile"; 
    private SQLiteDatabase mDataBase; 
    private final Context mContext; 

    public DataBaseHelper(Context context) { 
     super(context, DB_NAME, null, 1); 
     DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; 
     this.mContext = context; 
    } 

    public void createDataBase() throws IOException { 
     // If database not exists copy it from the assets 

     boolean mDataBaseExist = checkDataBase(); 
     if (!mDataBaseExist) { 
      this.getReadableDatabase(); 
      this.close(); 
      try { 
       copyDataBase(); 
       Log.e(TAG, "createDatabase database created"); 
      } catch (IOException mIOException) { 
       throw new Error("ErrorCopyingDataBase"); 
      } 
     } 
    } 

    private boolean checkDataBase() { 
     File dbFile = new File(DB_PATH + DB_NAME); 
     // Log.v("dbFile", dbFile + " "+ dbFile.exists()); 
     return dbFile.exists(); 
    } 


    private void copyDataBase() throws IOException { 
     InputStream mInput = mContext.getAssets().open(DB_NAME); 
     String outFileName = DB_PATH + DB_NAME; 
     OutputStream mOutput = new FileOutputStream(outFileName); 
     byte[] mBuffer = new byte[1024]; 
     int mLength; 
     while ((mLength = mInput.read(mBuffer)) > 0) { 
      mOutput.write(mBuffer, 0, mLength); 
     } 
     mOutput.flush(); 
     mOutput.close(); 
     mInput.close(); 
    } 

    public boolean openDataBase() throws SQLException { 
     String mPath = DB_PATH + DB_NAME; 
     // Log.v("mPath", mPath); 
     mDataBase = SQLiteDatabase.openDatabase(mPath, null, 
       SQLiteDatabase.CREATE_IF_NECESSARY); 
     // mDataBase = SQLiteDatabase.openDatabase(mPath, null, 
     // SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
     return mDataBase != null; 
    } 

    @Override 
    public synchronized void close() { 
     if (mDataBase != null) 
      mDataBase.close(); 
     super.close(); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 

    } 

} 

我怎麼能當我在PlayStore上升級應用程序時,避免此問題,而不重命名數據庫文件?

回答

3

我有使用保存在文件夾中的資產

沒有,你有一個應用程序V1本地數據庫的應用程序1.0版。 0表示複製數據庫「保存在資產文件夾中」並從中創建本地數據庫。希望,you use SQLiteAssetHelper for this

爲什麼資產文件沒有用新版本更新?

資產文件更新爲。你沒有做任何事來修復你所做的副本。如果您使用SQLiteAssetHelper,它具有處理此類升級的過程。

+0

好的。我更新了我的問題。 – KinkLore

+0

@KinkLore:我建議用'SQLiteAssetHelper'直接替換那個代碼。 – CommonsWare

0

是否每次更改數據庫時都更新DB助手類中的DATABASE_VERSION參數?檢查一下。它通常是在輔助類定義是這樣的:

private static final int DATABASE_VERSION = 1; 
+0

我會試試這個...但是我很困惑。當應用程序被更新並覆蓋時,資產文件中的dbfile以及由此文件加載的數據庫,爲什麼不更新? – KinkLore

+0

我的回答只是需要正確處理數據庫升級的一部分內容(假設你正確地完成了其餘部分)。我同意上面的CommonsWare的詳細解釋。我建議你按照http://joshhendo.blogspot.com/2012/03/android-sqlite-database-upgrade.html這篇文章進行比較,並與你在代碼中做的事情進行比較。本文將幫助您瞭解升級工作的過程。 –

+0

謝謝。我更新了我的問題。 – KinkLore

相關問題