android - 如何导入sqlite数据库android

标签 android database sqlite android-sdcard

当我在我使用的 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/

相关文章:

sql - 如何获取给定日期范围内当天至少有一个事件的所有日期?

android - 为什么 Google 不使用 Golang 或 python 作为 android 的语言?

android - 如何在android中显示全屏 fragment Activity ?

android - 销毁 Activity 的线程和 View 时会发生什么情况?

database - 基于键值的数据库,有人可以向我解释如何实际使用它们吗?

mongodb - mongodb中的查询 View

linux - 在centos中安装sqlite3 dev和其他包

sql - 将具有多个表的 SQLite 连接转换为 PostgreSQL

android - Gradle 同步中的错误 [IntelliJ Idea 13.1]

ruby-on-rails - rails : How to get all of the records that have the maximum count of a relation with a where clause