gpt4 book ai didi

android - 如何导入sqlite数据库android

转载 作者:行者123 更新时间:2023-11-30 03:56:43 28 4
gpt4 key购买 nike

当我在我使用的 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卡导入数据库的另一种方法,请告诉,谢谢

最佳答案

您的代码不清楚。你真的使用 SQLiteOpenHelper 的任何子类吗?如果是这样,您唯一需要做的就是确保在帮助器构造函数中提供正确的目标数据库版本,如果当前版本号和目标版本号不匹配,打开的帮助器将在运行 onUpgrade 时做出正确 react 。

关于android - 如何导入sqlite数据库android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13222533/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com