Reputation: 159
Currently I'm working on a android application that is shipped with a pre-populated database. The pre-populated db is in the assets folder and when it's first running db is copied to the device data folder.
The Database name is "Events.db" & there are 3 tables in that pre-populated database. They are EVENTS, USER_EVENTS & NOTIFICATIONS. EVENTSTable has 240 records, NOTIFICATIONS Table has 1 record & USER_EVENTS Table is Empty. That USER_EVENTS Empty table is populated when user save some data. In the future, when i want to add more rows to the EVENTS Table without affecting other table data, i need to find a way. Just to update EVENTS Table without affecting user entered data in the USER_EVENTS Table. So i was try to to change the DATABASE VERSION Number and, When it changes onUpgrade method is triggering & try to achieve what i want. But i haven't any idea how to do that.Is there anyway to achieve this kind of scenario? Or any suggestion with an example?
Currently these are the methods in my SqliteopenHelper class.
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DB_PATH = "/data/data/com.calendar.shanu.calendar/databases/";
private static final String DB_NAME = "Events.db";
private static final int DB_VERSION = 1;
private final Context myContext;
public SQLiteDatabase db;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
this.getWritableDatabase();
} else {
this.getWritableDatabase();
try {
this.close();
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
//checkDB.setVersion(DB_VERSION);
} catch (SQLiteException e) {
}
if (checkDB != null)
checkDB.close();
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[2048];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
//myDataBase.setVersion(DATABASE_VERSION);
}
public void opendb() throws SQLException {
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
Log.d("Test", "Database version: " +db.getVersion());
}
@Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion > oldVersion){
myContext.deleteDatabase(DB_NAME);
try {
copyDataBase();
Log.d("CopyDB", "in onUpgrade. Old is: " + oldVersion + " New is: " + newVersion);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void closedb(){
if (db!=null){
db.close();
}
}
//Database Insert, Update, Delete methods are in the below
}
Any help will be really appreciated.
Upvotes: 0
Views: 65
Reputation: 56953
Is there anyway to achieve this kind of scenario?
Yes, perhaps not ideal though, but in principle :-
Perhaps all initiated from onUpgrade after increasing the version number.
can you please show me a simple sample code. i think copy data using a cursor is a good approach, but I didnt understand how to read tables from two tables.
Here's an example of doing the whole thing including the copy from the assets
This assumes that the original database is named events.db and has 3 tables namely event_table1 (populated), event_table2 (also populated) and event_table3 empty.
The new database, with event_table3 populated is called events_extra.db and has been placed into the assets folder.
One class (couldn't be bothered using a subclass of SQliteOpenHelper) called ImportFromSecondaryDB copies the DB from the assets folder as a second database, opens both, extracts all the rows from the secondary DB (the one just copied from the assets) 3rd table and inserts rows into the original DB. Lastly it deletes all the rows from the secondary database.
public class ImportFromSecondaryDB {
public static final int IFSDB_COMPLETIONCODE_ALLREADYDONE = 1;
public static final int IFSDB_COMPLETIONCODE_ERRORCOPYINGDBFROMASSET = 2;
SQLiteDatabase mPrimaryDB;
SQLiteDatabase mSecondaryDB;
int mCompletionCode = 0;
int stage = 0;
ImportFromSecondaryDB(Context context, String primaryDBName, String secondaryDBName) {
File primaryDBFile = new File(context.getDatabasePath(primaryDBName).getPath());
File secondaryDBFile = new File(context.getDatabasePath(secondaryDBName).getPath());
byte[] buffer = new byte[4096];
if (!primaryDBFile.exists()) {
throw new RuntimeException("Unable to Import as the Primary Database does not exist!");
}
if (secondaryDBFile.exists()) {
mCompletionCode = IFSDB_COMPLETIONCODE_ALLREADYDONE;
return;
}
InputStream is;
OutputStream os;
int length = 0;
int copied = 0;
try {
is = context.getAssets().open(secondaryDBName);
stage++;
os = new FileOutputStream(secondaryDBFile);
stage++;
while ((length = is.read(buffer)) > 0) {
copied = copied + length;
os.write(buffer);
}
stage++;
os.flush();
stage++;
os.close();
stage++;
is.close();
stage++;
} catch (IOException e) {
e.printStackTrace();
mCompletionCode = IFSDB_COMPLETIONCODE_ERRORCOPYINGDBFROMASSET;
throw new RuntimeException(
"Error copying secondary database from Asset " + secondaryDBName +
" to " + secondaryDBFile.getPath() + " at stage " + String.valueOf(stage)
);
}
mPrimaryDB = SQLiteDatabase.openDatabase(primaryDBFile.getPath(),null,SQLiteDatabase.OPEN_READWRITE);
mSecondaryDB = SQLiteDatabase.openDatabase(secondaryDBFile.getPath(),null,SQLiteDatabase.OPEN_READWRITE);
ContentValues cv = new ContentValues();
Cursor csr = mSecondaryDB.query("event_table3",null,null,null,null,null,null);
mPrimaryDB.beginTransaction();
while (csr.moveToNext()) {
cv.clear();
cv.put("name",csr.getString(csr.getColumnIndex("name")));
mPrimaryDB.insert("event_table3",null,cv);
}
mPrimaryDB.setTransactionSuccessful();
mPrimaryDB.endTransaction();
mSecondaryDB.delete("event_table3",null,null); //???????
mPrimaryDB.close();
mSecondaryDB.close();
}
public int getCompletionCode() {
return this.mCompletionCode;
}
}
The following is the code that was used to invoke the above from an Activity :-
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dumpAllThreeTables(); // Dump Existing tables
ImportFromSecondaryDB mIFSDB = new ImportFromSecondaryDB(this,"events.db","events_extra.db"); // Do the import
dumpAllThreeTables(); // Dump the changed tables
}
private void dumpAllThreeTables() {
SQLiteDatabase mainDB = SQLiteDatabase.openDatabase(this.getDatabasePath("events.db").getPath(),null,SQLiteDatabase.OPEN_READWRITE);
Cursor csr = mainDB.query("event_table1",null,null,null,null,null,null);
DatabaseUtils.dumpCursor(csr);
csr = mainDB.query("event_table2",null,null,null,null,null,null);
DatabaseUtils.dumpCursor(csr);
csr = mainDB.query("event_table3",null,null,null,null,null,null);
DatabaseUtils.dumpCursor(csr);
csr.close();
mainDB.close();
}
}
The first set of dumped tables (not the end nothing in event_table3) :-
12-12 10:43:38.766 2842-2842/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@534c2808
12-12 10:43:38.770 2842-2842/? I/System.out: 0 {
12-12 10:43:38.770 2842-2842/? I/System.out: id=1
12-12 10:43:38.770 2842-2842/? I/System.out: mainname=A
12-12 10:43:38.770 2842-2842/? I/System.out: }
12-12 10:43:38.770 2842-2842/? I/System.out: 1 {
12-12 10:43:38.770 2842-2842/? I/System.out: id=2
12-12 10:43:38.770 2842-2842/? I/System.out: mainname=B
12-12 10:43:38.770 2842-2842/? I/System.out: }
12-12 10:43:38.770 2842-2842/? I/System.out: 2 {
12-12 10:43:38.770 2842-2842/? I/System.out: id=3
12-12 10:43:38.770 2842-2842/? I/System.out: mainname=C
12-12 10:43:38.770 2842-2842/? I/System.out: }
12-12 10:43:38.770 2842-2842/? I/System.out: 3 {
12-12 10:43:38.770 2842-2842/? I/System.out: id=4
12-12 10:43:38.770 2842-2842/? I/System.out: mainname=D
12-12 10:43:38.770 2842-2842/? I/System.out: }
12-12 10:43:38.770 2842-2842/? I/System.out: 4 {
12-12 10:43:38.770 2842-2842/? I/System.out: id=5
12-12 10:43:38.770 2842-2842/? I/System.out: mainname=E
12-12 10:43:38.770 2842-2842/? I/System.out: }
12-12 10:43:38.770 2842-2842/? I/System.out: 5 {
12-12 10:43:38.770 2842-2842/? I/System.out: id=6
12-12 10:43:38.770 2842-2842/? I/System.out: mainname=F
12-12 10:43:38.770 2842-2842/? I/System.out: }
12-12 10:43:38.770 2842-2842/? I/System.out: 6 {
12-12 10:43:38.770 2842-2842/? I/System.out: id=7
12-12 10:43:38.770 2842-2842/? I/System.out: mainname=G
12-12 10:43:38.770 2842-2842/? I/System.out: }
12-12 10:43:38.770 2842-2842/? I/System.out: 7 {
12-12 10:43:38.770 2842-2842/? I/System.out: id=8
12-12 10:43:38.770 2842-2842/? I/System.out: mainname=H
12-12 10:43:38.770 2842-2842/? I/System.out: }
12-12 10:43:38.770 2842-2842/? I/System.out: 8 {
12-12 10:43:38.770 2842-2842/? I/System.out: id=9
12-12 10:43:38.770 2842-2842/? I/System.out: mainname=I
12-12 10:43:38.770 2842-2842/? I/System.out: }
12-12 10:43:38.770 2842-2842/? I/System.out: <<<<<
12-12 10:43:38.770 2842-2842/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@534af7a8
12-12 10:43:38.770 2842-2842/? I/System.out: 0 {
12-12 10:43:38.770 2842-2842/? I/System.out: id=1
12-12 10:43:38.770 2842-2842/? I/System.out: secondaryname=Horse
12-12 10:43:38.770 2842-2842/? I/System.out: }
12-12 10:43:38.770 2842-2842/? I/System.out: 1 {
12-12 10:43:38.770 2842-2842/? I/System.out: id=2
12-12 10:43:38.770 2842-2842/? I/System.out: secondaryname=Cow
12-12 10:43:38.770 2842-2842/? I/System.out: }
12-12 10:43:38.770 2842-2842/? I/System.out: 2 {
12-12 10:43:38.770 2842-2842/? I/System.out: id=3
12-12 10:43:38.774 2842-2842/? I/System.out: secondaryname=Rabbit
12-12 10:43:38.774 2842-2842/? I/System.out: }
12-12 10:43:38.774 2842-2842/? I/System.out: 3 {
12-12 10:43:38.774 2842-2842/? I/System.out: id=4
12-12 10:43:38.774 2842-2842/? I/System.out: secondaryname=Hare
12-12 10:43:38.774 2842-2842/? I/System.out: }
12-12 10:43:38.774 2842-2842/? I/System.out: 4 {
12-12 10:43:38.774 2842-2842/? I/System.out: id=5
12-12 10:43:38.774 2842-2842/? I/System.out: secondaryname=Cat
12-12 10:43:38.774 2842-2842/? I/System.out: }
12-12 10:43:38.774 2842-2842/? I/System.out: 5 {
12-12 10:43:38.774 2842-2842/? I/System.out: id=6
12-12 10:43:38.774 2842-2842/? I/System.out: secondaryname=Dog
12-12 10:43:38.774 2842-2842/? I/System.out: }
12-12 10:43:38.774 2842-2842/? I/System.out: 6 {
12-12 10:43:38.774 2842-2842/? I/System.out: id=7
12-12 10:43:38.774 2842-2842/? I/System.out: secondaryname=Porcupine
12-12 10:43:38.774 2842-2842/? I/System.out: }
12-12 10:43:38.774 2842-2842/? I/System.out: 7 {
12-12 10:43:38.774 2842-2842/? I/System.out: id=8
12-12 10:43:38.774 2842-2842/? I/System.out: secondaryname=Elephant
12-12 10:43:38.774 2842-2842/? I/System.out: }
12-12 10:43:38.774 2842-2842/? I/System.out: 8 {
12-12 10:43:38.774 2842-2842/? I/System.out: id=9
12-12 10:43:38.774 2842-2842/? I/System.out: secondaryname=Sheep
12-12 10:43:38.774 2842-2842/? I/System.out: }
12-12 10:43:38.774 2842-2842/? I/System.out: 9 {
12-12 10:43:38.774 2842-2842/? I/System.out: id=10
12-12 10:43:38.774 2842-2842/? I/System.out: secondaryname=Goat
12-12 10:43:38.774 2842-2842/? I/System.out: }
12-12 10:43:38.774 2842-2842/? I/System.out: <<<<<
12-12 10:43:38.774 2842-2842/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@534c6e1c
12-12 10:43:38.774 2842-2842/? I/System.out: <<<<<
The second set of dumped tables (see how event_table3 (last dump) now has data) :-
12-12 10:43:38.790 2842-2842/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@534ae284
12-12 10:43:38.790 2842-2842/? I/System.out: 0 {
12-12 10:43:38.790 2842-2842/? I/System.out: id=1
12-12 10:43:38.790 2842-2842/? I/System.out: mainname=A
12-12 10:43:38.790 2842-2842/? I/System.out: }
12-12 10:43:38.790 2842-2842/? I/System.out: 1 {
12-12 10:43:38.790 2842-2842/? I/System.out: id=2
12-12 10:43:38.790 2842-2842/? I/System.out: mainname=B
12-12 10:43:38.790 2842-2842/? I/System.out: }
12-12 10:43:38.790 2842-2842/? I/System.out: 2 {
12-12 10:43:38.790 2842-2842/? I/System.out: id=3
12-12 10:43:38.790 2842-2842/? I/System.out: mainname=C
12-12 10:43:38.790 2842-2842/? I/System.out: }
12-12 10:43:38.790 2842-2842/? I/System.out: 3 {
12-12 10:43:38.790 2842-2842/? I/System.out: id=4
12-12 10:43:38.790 2842-2842/? I/System.out: mainname=D
12-12 10:43:38.790 2842-2842/? I/System.out: }
12-12 10:43:38.790 2842-2842/? I/System.out: 4 {
12-12 10:43:38.790 2842-2842/? I/System.out: id=5
12-12 10:43:38.790 2842-2842/? I/System.out: mainname=E
12-12 10:43:38.790 2842-2842/? I/System.out: }
12-12 10:43:38.790 2842-2842/? I/System.out: 5 {
12-12 10:43:38.790 2842-2842/? I/System.out: id=6
12-12 10:43:38.790 2842-2842/? I/System.out: mainname=F
12-12 10:43:38.790 2842-2842/? I/System.out: }
12-12 10:43:38.790 2842-2842/? I/System.out: 6 {
12-12 10:43:38.790 2842-2842/? I/System.out: id=7
12-12 10:43:38.790 2842-2842/? I/System.out: mainname=G
12-12 10:43:38.790 2842-2842/? I/System.out: }
12-12 10:43:38.790 2842-2842/? I/System.out: 7 {
12-12 10:43:38.790 2842-2842/? I/System.out: id=8
12-12 10:43:38.790 2842-2842/? I/System.out: mainname=H
12-12 10:43:38.790 2842-2842/? I/System.out: }
12-12 10:43:38.790 2842-2842/? I/System.out: 8 {
12-12 10:43:38.790 2842-2842/? I/System.out: id=9
12-12 10:43:38.790 2842-2842/? I/System.out: mainname=I
12-12 10:43:38.790 2842-2842/? I/System.out: }
12-12 10:43:38.790 2842-2842/? I/System.out: <<<<<
12-12 10:43:38.790 2842-2842/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@534c44b4
12-12 10:43:38.794 2842-2842/? I/System.out: 0 {
12-12 10:43:38.794 2842-2842/? I/System.out: id=1
12-12 10:43:38.794 2842-2842/? I/System.out: secondaryname=Horse
12-12 10:43:38.794 2842-2842/? I/System.out: }
12-12 10:43:38.794 2842-2842/? I/System.out: 1 {
12-12 10:43:38.794 2842-2842/? I/System.out: id=2
12-12 10:43:38.794 2842-2842/? I/System.out: secondaryname=Cow
12-12 10:43:38.794 2842-2842/? I/System.out: }
12-12 10:43:38.794 2842-2842/? I/System.out: 2 {
12-12 10:43:38.794 2842-2842/? I/System.out: id=3
12-12 10:43:38.794 2842-2842/? I/System.out: secondaryname=Rabbit
12-12 10:43:38.794 2842-2842/? I/System.out: }
12-12 10:43:38.794 2842-2842/? I/System.out: 3 {
12-12 10:43:38.794 2842-2842/? I/System.out: id=4
12-12 10:43:38.794 2842-2842/? I/System.out: secondaryname=Hare
12-12 10:43:38.794 2842-2842/? I/System.out: }
12-12 10:43:38.794 2842-2842/? I/System.out: 4 {
12-12 10:43:38.794 2842-2842/? I/System.out: id=5
12-12 10:43:38.794 2842-2842/? I/System.out: secondaryname=Cat
12-12 10:43:38.794 2842-2842/? I/System.out: }
12-12 10:43:38.794 2842-2842/? I/System.out: 5 {
12-12 10:43:38.794 2842-2842/? I/System.out: id=6
12-12 10:43:38.794 2842-2842/? I/System.out: secondaryname=Dog
12-12 10:43:38.794 2842-2842/? I/System.out: }
12-12 10:43:38.794 2842-2842/? I/System.out: 6 {
12-12 10:43:38.794 2842-2842/? I/System.out: id=7
12-12 10:43:38.798 2842-2842/? I/System.out: secondaryname=Porcupine
12-12 10:43:38.798 2842-2842/? I/System.out: }
12-12 10:43:38.798 2842-2842/? I/System.out: 7 {
12-12 10:43:38.798 2842-2842/? I/System.out: id=8
12-12 10:43:38.802 2842-2842/? I/System.out: secondaryname=Elephant
12-12 10:43:38.802 2842-2842/? I/System.out: }
12-12 10:43:38.802 2842-2842/? I/System.out: 8 {
12-12 10:43:38.802 2842-2842/? I/System.out: id=9
12-12 10:43:38.802 2842-2842/? I/System.out: secondaryname=Sheep
12-12 10:43:38.802 2842-2842/? I/System.out: }
12-12 10:43:38.802 2842-2842/? I/System.out: 9 {
12-12 10:43:38.802 2842-2842/? I/System.out: id=10
12-12 10:43:38.802 2842-2842/? I/System.out: secondaryname=Goat
12-12 10:43:38.802 2842-2842/? I/System.out: }
12-12 10:43:38.802 2842-2842/? I/System.out: <<<<<
12-12 10:43:38.802 2842-2842/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@534c48c4
12-12 10:43:38.802 2842-2842/? I/System.out: 0 {
12-12 10:43:38.802 2842-2842/? I/System.out: id=1
12-12 10:43:38.802 2842-2842/? I/System.out: name=Z
12-12 10:43:38.802 2842-2842/? I/System.out: }
12-12 10:43:38.802 2842-2842/? I/System.out: 1 {
12-12 10:43:38.802 2842-2842/? I/System.out: id=2
12-12 10:43:38.802 2842-2842/? I/System.out: name=X
12-12 10:43:38.802 2842-2842/? I/System.out: }
12-12 10:43:38.802 2842-2842/? I/System.out: 2 {
12-12 10:43:38.802 2842-2842/? I/System.out: id=3
12-12 10:43:38.802 2842-2842/? I/System.out: name=Y
12-12 10:43:38.802 2842-2842/? I/System.out: }
12-12 10:43:38.802 2842-2842/? I/System.out: 3 {
12-12 10:43:38.802 2842-2842/? I/System.out: id=4
12-12 10:43:38.802 2842-2842/? I/System.out: name=W
12-12 10:43:38.802 2842-2842/? I/System.out: }
12-12 10:43:38.802 2842-2842/? I/System.out: 4 {
12-12 10:43:38.802 2842-2842/? I/System.out: id=5
12-12 10:43:38.802 2842-2842/? I/System.out: name=U
12-12 10:43:38.802 2842-2842/? I/System.out: }
12-12 10:43:38.802 2842-2842/? I/System.out: 5 {
12-12 10:43:38.802 2842-2842/? I/System.out: id=6
12-12 10:43:38.802 2842-2842/? I/System.out: name=R
12-12 10:43:38.802 2842-2842/? I/System.out: }
12-12 10:43:38.802 2842-2842/? I/System.out: 6 {
12-12 10:43:38.802 2842-2842/? I/System.out: id=7
12-12 10:43:38.802 2842-2842/? I/System.out: name=S
12-12 10:43:38.802 2842-2842/? I/System.out: }
12-12 10:43:38.802 2842-2842/? I/System.out: 7 {
12-12 10:43:38.802 2842-2842/? I/System.out: id=8
12-12 10:43:38.802 2842-2842/? I/System.out: name=T
12-12 10:43:38.802 2842-2842/? I/System.out: }
12-12 10:43:38.802 2842-2842/? I/System.out: <<<<<
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DB_PATH = "/data/data/com.calendar.shanu.calendar/databases/";
private static final String DB_NAME = "events.db"; //<<<<<<<<<<< Changed for testing
private static final int DB_VERSION = 2; //<<<<<<<<<< Changed for Testing
private final Context myContext;
public SQLiteDatabase db;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
this.getWritableDatabase();
} else {
this.getWritableDatabase();
try {
this.close();
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
//checkDB.setVersion(DB_VERSION);
} catch (SQLiteException e) {
}
if (checkDB != null)
checkDB.close();
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[2048];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
//myDataBase.setVersion(DATABASE_VERSION);
}
public void opendb() throws SQLException {
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
Log.d("Test", "Database version: " +db.getVersion());
}
@Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion > oldVersion){
importFromSecondaryDB(myContext,db,"events_extra.db"); //<<<<<<<<<<
}
}
public void closedb(){
if (db!=null){
db.close();
}
}
private int importFromSecondaryDB(Context context, SQLiteDatabase maindb, String secondaryDBName) {
File primaryDBFile = new File(maindb.getPath());
File secondaryDBFile = new File(context.getDatabasePath(secondaryDBName).getPath());
byte[] buffer = new byte[4096];
// Check that the main database exist, if not then throw a runtime exception
if (!primaryDBFile.exists()) {
throw new RuntimeException("Unable to Import as the Primary Database does not exist!");
}
// If the secondary database exists then it has already been copied and thus data has been loaded
// so return (can check returned code for allready done)
if (secondaryDBFile.exists()) {
return 1;
}
//Prepare for file handling (copy)
InputStream is;
OutputStream os;
int length;
int copied = 0;
try {
is = context.getAssets().open(secondaryDBName); // Get the assets file
os = new FileOutputStream(secondaryDBFile); // Get the file to be written to
// Loop though the asset file in chunks reading it and then writing to output
while ((length = is.read(buffer)) > 0) {
copied = copied + length;
os.write(buffer);
}
// Copy done so flush and close files
os.flush();
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(
"Error copying secondary database from Asset " + secondaryDBName +
" to " + secondaryDBFile.getPath()
);
}
// Open the newly copied database
SQLiteDatabase mSecondaryDB = SQLiteDatabase.openDatabase(secondaryDBFile.getPath(),null,SQLiteDatabase.OPEN_READWRITE);
//Copy the rows from the secondary (new) database, from table event_table3 to the main database's event_table3
//<<<<<<<<<< Note will have to be tailored to suit. >>>>>>>>>>>
ContentValues cv = new ContentValues();
// Extract the tables
Cursor csr = mSecondaryDB.query("event_table3",null,null,null,null,null,null);
// Do in a Transaction
maindb.beginTransaction();
// Loop through extracted rows
while (csr.moveToNext()) {
cv.clear();
cv.put("name",csr.getString(csr.getColumnIndex("name")));
maindb.insert("event_table3",null,cv);
}
// ALl done so commit and close the transaction
maindb.setTransactionSuccessful();
maindb.endTransaction();
// OPTIONAL delete the rows from the secondary table as they have been applied
mSecondaryDB.delete("event_table3",null,null); //???????
// Finally close the secondary database as done with it (main stays open)
mSecondaryDB.close();
return 0;
}
}
Upvotes: 1