Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem With Updating Database in Android app
    primarykey
    data
    text
    <p>I'm having a bit of trouble understand what to do if I want to upgrade a database with an app update, though my case is a bit unique. The starting database (in the assets folder) has a few tables that have records in them already, so it just directly copies the entire database to the data/data/<em>package</em>/databases/ folder. When I want to do an update I want some some tables to keep their data, but I want other tables to be dropped and added from the updated database.</p> <p>I'm assuming I put my code in OnUpgrade, but I'm not sure what to put...I can back up the data in the table, but I'm not sure how copy it back.</p> <p>Here's some snippets of my code.</p> <pre><code>public class DataBaseHelper extends SQLiteOpenHelper{ //The Android's default system path of your application database. private static String DB_PATH = "/data/data/package/databases/"; private static String DB_NAME = "drink.db"; private static final int DATABASE_VERSION = 2; SQLiteDatabase myDataBase; private final Context myContext; public DataBaseHelper(Context context) { super(context, DB_NAME, null, DATABASE_VERSION); this.myContext = context; } public void createDataBase() throws IOException{ boolean dbExist = checkDataBase(); if(dbExist){ //do nothing database already exists }else{ try { copyDataBase(); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } } } private boolean checkDataBase(){ SQLiteDatabase checkDB = null; try{ String myPath = DB_PATH + DB_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); }catch(SQLiteException e){ //database does't exist yet. } if(checkDB != null){ checkDB.close(); } return checkDB != null ? true : false; } private void copyDataBase() throws IOException{ //Open your local db as the input stream InputStream myInput = myContext.getAssets().open(DB_NAME); // Path to the just created empty db String outFileName = DB_PATH + DB_NAME; //Open the empty db as the output stream OutputStream myOutput = new FileOutputStream(outFileName); //transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))&gt;0){ myOutput.write(buffer, 0, length); } //Close the streams myOutput.flush(); myOutput.close(); myInput.close(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if (newVersion &gt; oldVersion) { } } } </code></pre> <p>Thanks again!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload