2

I have an SQLite DB generated from a file in the asset folder... Now I know that is not possible to modify the resource of the app, but if I release an updated app I want that the already created database will be cleared and re-created from the new asset file DB included in the new version of the app

This is my Database Helper class

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

    }

}

The line

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

should identify the version ad if the already present DB has a version number previous must be updated,

so if I'll release a new app with DB version 3

super(context, nomeDB, null, 3);

the app must recreates the DB from the assets included in the new compiled version of the app.

But I don't know a right way to do this.

AndreaF
  • 11,975
  • 27
  • 102
  • 168

2 Answers2

2

You need to use the built-in lifecycle methods for onCreate() and onUpdate().

Something like this:

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;
        }
    }

}
Corey Scott
  • 2,430
  • 3
  • 28
  • 33
0

you can use the android-sqlite-asset-helper to update the database or point to latest version

https://github.com/jgilfelt/android-sqlite-asset-helper

Sam
  • 6,215
  • 9
  • 71
  • 90