当前,我正在使用预填充数据库附带的android应用程序。预先填充的数据库位于assets文件夹中,并且在其首次运行时将其复制到设备数据文件夹中。
数据库名称为“ Events.db”,该预填充数据库中有3个表。它们是事件,USER_EVENTS和通知。 EVENTSTable有240条记录,NOTIFICATIONS表有1条记录,并且USER_EVENTS表为空。用户保存一些数据时,将填充USER_EVENTS空表。将来,当我想向活动表中添加更多行而不影响其他表数据时,我需要找到一种方法。仅用于更新EVENTS表,而不影响USER_EVENTS表中用户输入的数据。因此,我试图更改数据库版本号,并且当它更改时,onUpgrade方法将触发并尝试实现我想要的功能。但是我不知道该怎么做,反正有实现这种情况的方法吗?或任何带有示例的建议?
当前这些是我的SqliteopenHelper类中的方法。
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
}
任何帮助将不胜感激。
答案 0 :(得分:1)
反正有实现这种情况的方法吗?
是的,虽然可能并不理想,但原则上:-
也许所有这些都是在增加版本号之后从 onUpgrade 启动的。
请给我看一个简单的示例代码。我认为使用复制数据 游标是一种好方法,但我不明白如何读取表格 来自两个表。
这是一个完整的示例,包括资产的副本
这假定原始数据库名为 events.db ,并具有3个表,即 event_table1 (已填充), event_table2 (也已填充)和 event_table3 为空。
填充了 event_table3 的新数据库称为 events_extra.db ,并已放置在资产文件夹中。
一个名为 ImportFromSecondaryDB 的类(不能使用SQliteOpenHelper的子类来打扰),将资产文件夹中的数据库复制为第二个数据库,同时打开两个数据库,并从第二个数据库中提取所有行(第三个表,然后将行插入到原始数据库中。最后,它从辅助数据库中删除所有行。
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;
}
}
以下是用于从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();
}
}
第一组转储表(在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: <<<<<
第二组转储表(请参阅event_table3(最后一个转储)现在如何获取数据):-
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;
}
}