【问题标题】:Assets folder files aren't updated when I update the app更新应用程序时未更新资产文件夹文件
【发布时间】:2012-12-24 06:19:23
【问题描述】:

我有一个应用程序 v1.0,它使用保存在 assets 文件夹中的本地数据库...我已将我的应用程序更新到 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 上升级应用程序而不重命名 db 文件时,如何避免这个问题?

【问题讨论】:

    标签: android apk android-assets


    【解决方案1】:

    我有一个应用程序 v1.0,它使用保存在 assets 文件夹中的本地数据库

    不,您有一个应用程序 v1.0,它复制一个“保存在 assets 文件夹中”的数据库并从中创建一个本地数据库。希望you use SQLiteAssetHelper for this。

    为什么资产文件没有更新到新版本?

    资产文件已更新。您没有做任何事情来修复您制作的副本。如果您使用SQLiteAssetHelper,它有一个处理此类升级的过程。

    【讨论】:

    • @KinkLore:我建议将该代码直接替换为 SQLiteAssetHelper。
    【解决方案2】:

    您是否在每次更改数据库时都更新 DB 助手类中的 DATABASE_VERSION 参数?检查那个。它通常在辅助类中定义如下:

    private static final int DATABASE_VERSION = 1;
    

    【讨论】:

    • 我会试试这个...但是我很困惑。当应用程序更新和覆盖时,资产文件中的 dbfile 以及从该文件加载的数据库,为什么没有更新?
    • 我的回答只是为了处理数据库升级需要正确完成的部分事情(假设您正确完成了其余部分)。我同意上面 CommonsWare 的详细解释。我建议您关注文章joshhendo.blogspot.com/2012/03/… 并将其与您在代码中所做的事情进行比较。本文将帮助您了解升级的过程。
    猜你喜欢
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多