android - 从 Assets 文件夹创建的 SQLite DB 更新问题

标签 android sqlite android-sqlite

我有一个从 Assets 文件夹中的文件生成的 SQLite 数据库... 现在我知道不可能修改应用程序的资源, 但如果我发布更新的应用程序,我希望已创建的数据库将被清除并从新版本应用程序中包含的新 Assets 文件数据库重新创建

这是我的数据库助手类

public class DataBaseHelper extends SQLiteOpenHelper {
    private static String TAG = "DataBaseHelper"; // Tag just for the LogCat
                                                    // window
    // destination path (location) of our database on device
    private static String DB_PATH = "";
    private static String DB_NAME = "";// Database name
    private SQLiteDatabase mDataBase;
    private final Context mContext;

    public DataBaseHelper(Context context, String nomeDB) {
        super(context, nomeDB, null, 2);// 2 is the Database Version
        DB_NAME = nomeDB;
        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 {
                // Copy the db from assests
                copyDataBase();
                Log.e(TAG, "createDatabase database created");
            } catch (IOException mIOException) {
                Log.e(TAG, mIOException.toString());
                throw mIOException;
            }
        }
    }

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

    // Copy DB from assests
    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;
        mDataBase = SQLiteDatabase.openDatabase(mPath, null,
                SQLiteDatabase.CREATE_IF_NECESSARY);
        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

    }

}

线路

super(context, nomeDB, null, 2);// 2 is the Database Version

如果已存在的数据库具有之前必须更新的版本号,则应识别版本广告,

所以如果我发布一个带有 DB 版本 3 的新应用

super(context, nomeDB, null, 3);

应用程序必须从应用程序的新编译版本中包含的 Assets 重新创建数据库。

但我不知道正确的方法。

最佳答案

您需要使用 onCreate() 和 onUpdate() 的内置生命周期方法。

类似这样的事情:

public class DataBaseHelper extends SQLiteOpenHelper {
    private static String TAG = "DataBaseHelper"; // Tag just for the LogCat
                                                    // window
    // destination path (location) of our database on device
    private static final int    DATABASE_VERSION = 3;
    private static String DB_PATH = "";
    private static String DB_NAME = "";// Database name
    private SQLiteDatabase mDataBase;
    private final Context mContext;

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

    // Copy DB from assests
    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;
        mDataBase = SQLiteDatabase.openDatabase(mPath, null,
                SQLiteDatabase.CREATE_IF_NECESSARY);
        return mDataBase != null;
    }

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

    /**
     * This method is called when the app doesn't have a database and a new one needs to be created
     */
    @Override
    public void onCreate(SQLiteDatabase arg0) {
        try {
            // Copy the db from assests
            copyDataBase();
            Log.e(TAG, "database created");
        } catch (IOException mIOException) {
            Log.e(TAG, mIOException.toString());
            throw mIOException;
        }
   }

    /**
     * This method is called when the app's database version is < the value passed into the constructor
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        try {
            // delete existing?

            // Copy the db from assests
            copyDataBase();
            Log.e(TAG, "database updated");
        } catch (IOException mIOException) {
            Log.e(TAG, mIOException.toString());
            throw mIOException;
        }
    }

}

关于android - 从 Assets 文件夹创建的 SQLite DB 更新问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15328390/

相关文章:

安卓升级应用

java - ZipException : transform duplicate entry com/google/android/gms/internal/zzez. 类

java - 每次插入数据库时​​如何找到数据字段(例如电子邮件)的唯一性?

android - SQLite 的内存消耗?

Android项目停止识别编译器

android - 测试 LiveData 转换?

python - 如何在插入时生成带有列名称的转储 sqlite3?

android.database.sqlite.SQLiteCantOpenDatabaseException : unknown error (code 14): Could not open database

android - 如何在android中正确关闭光标

java - 无法在我的应用程序中删除 SQLite 数据库中的一行