【问题标题】:Replace database with backup from dropbox用 Dropbox 中的备份替换数据库
【发布时间】:2014-01-10 01:23:30
【问题描述】:

我正在尝试在 Dropbox 上备份我的数据库版本。将文件上传到保管箱时一切正常,但我无法用保管箱上的文件替换当前手机上的文件。

    new Thread(new Runnable() {
        public void run() {
            File file = new File(getDatabasePath("my_database").getPath());
            file.getParentFile().mkdirs();

            try {
                FileOutputStream outputStream = new FileOutputStream(file);
                DropboxFileInfo info =dbInstance.getFile("/BackupFolder/my_database", null,
                        outputStream, null);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }).start();

操作完成后,我收到很多“SQLiteException: no such table”错误。我已经验证了dropbox中的数据库是正确的。

01-09 20:03:37.601: W/System.err(7185): android.database.sqlite.SQLiteException: no such table: transaction_table (code 1)
at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)
01-09 20:03:37.601: W/System.err(7185): at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:1036)
01-09 20:03:37.601: W/System.err(7185):     at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
01-09 20:03:37.601: W/System.err(7185):     at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
01-09 20:03:37.601: W/System.err(7185):     at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:144)
01-09 20:03:37.601: W/System.err(7185):     at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
01-09 20:03:37.601: W/System.err(7185):     at android.content.ContentResolver.query(ContentResolver.java:436)
01-09 20:03:37.601: W/System.err(7185):     at android.content.ContentResolver.query(ContentResolver.java:360)

更新

我已将文件下载到设备上的另一个位置,以便我可以使用 sqlite3 查看它,并且所有内容都在其中。因此,显然下载工作正常,但我无法在运行时使用此文件覆盖现有数据库。

【问题讨论】:

  • 请添加您的例外。
  • 我添加了查询时遇到的异常之一。
  • 检查数据库的名称和位置。并检查数据库文件是否存在。并打印文件大小,这将是有用的信息。
  • 我用新的发现更新了我的答案。
  • 在这个答案中,我解释了一种将 sqlite 文件复制到应用程序数据文件夹并将其用作主数据库 stackoverflow.com/questions/21101515/… 的安全方法

标签: android sqlite dropbox fileoutputstream


【解决方案1】:

在覆盖数据库之前尝试关闭所有数据库连接。

你可以这样做:

  • 关闭与旧数据库的连接。
  • 删除旧的数据库文件。
  • 移动或复制新数据库。
  • 创建与新数据库的新连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 2021-12-13
    • 2011-02-20
    相关资源
    最近更新 更多