在sd卡上导出数据库时,我使用
try {
File sd = new File(Environment.getExternalStorageDirectory()+"/ABC");
File data = Environment.getDataDirectory();
boolean success = false;
if (!sd.exists()) {
success = sd.mkdir();
}
if (sd.canWrite()) {
String currentDBPath = "\\data\\com.example.skurat\\databases\\Income.db";
String backupDBPath = "Income.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists() && success) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
}因此,文件"Income.db“存储在sd卡上。
当我从sd卡导入新数据库时,我使用
File dbfile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/ABC/Income.db" );
String backupDBPath = "Income.db";
File backupDB = new File(dbfile, backupDBPath);
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
// toDoDBAdapter = new DBAdapter(this, "Income.db"); written in the "onCreate" method, working correctly
toDoDBAdapter.dbHelper.onUpgrade(db, 1, 2);
// refresh screen, working correctly
populateTodoList();数据库也没有更新。为什么?我使用API 7
附注:
如果您知道从应用程序中sd卡导入数据库的另一种方法,请告诉我,谢谢
发布于 2012-11-05 08:19:23
没有导入数据库的代码。您的代码只是在SD卡上的数据库上调用onUpgrade。
您必须复制数据库文件,就像导出一样。或者,您必须将记录从该数据库复制到您的正常数据库中。
发布于 2012-11-04 20:54:23
你的代码不清楚。您是否实际使用SQLiteOpenHelper的任何子类?如果是这样的话,您唯一需要做的就是确保在helper构造函数中提供正确的目标数据库版本,如果当前版本号和目标版本号不匹配,打开的助手将正确地响应运行onUpgrade。
https://stackoverflow.com/questions/13222533
复制相似问题