2013-01-09 95 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

相关问题