【问题标题】:Backup and restore sqlite from disk to memory in Java在Java中将sqlite从磁盘备份和恢复到内存
【发布时间】:2013-07-07 01:09:00
【问题描述】:

我正在尝试将 sqlite-File 读入内存以获得更好的性能,当关闭我的应用程序时,我想将它写回硬盘。

我在 Java 中使用 jdbc (3.7.2) 驱动程序。

根据文档,我的代码看起来像

this._conn = DriverManager.getConnection("jdbc:sqlite:");
Statement stat  = this._conn.createStatement();
File dbFile = new File(this._config.GetDataBaseFile());
if (dbFile.exists()) {
    this._logger.AddInfo("File exists.");
    stat.executeUpdate("restore from " + dbFile.getAbsolutePath());
}

文件存在(并且它是一个有效的sqlite db),this._conn 已打开,但如果我想对其执行语句,似乎里面没有表也没有数据。它似乎没有恢复任何东西。

关于如何进一步解决/调试的任何建议?

(顺便说一句 - 如果我在连接上使用 stat.executeUpdate("backup to test.db"),它会备份我的空 :memory: db...)

【问题讨论】:

  • 该文件存在,并且它是一个有效的 SQLite 数据库 - 但它是否包含任何表?
  • 是的。还有数据。
  • @mjreaper..你找到这段代码的解决方案了吗?你能帮我恢复吗?

标签: java sqlite jdbc


【解决方案1】:

您似乎缺少两件事:1)文件名周围的引号,以及 2)stat.close。请尝试以下操作:

this._conn = DriverManager.getConnection("jdbc:sqlite:");
Statement stat  = this._conn.createStatement();
File dbFile = new File(this._config.GetDataBaseFile());
if (dbFile.exists()) {
    this._logger.AddInfo("File exists.");
    stat.executeUpdate("restore from '" + dbFile.getAbsolutePath() + "'");
    stat.close();
}

这是我让它与 Xerial SQLite JDBC 版本 3.7.15-M1 一起工作的唯一方法。在这种情况下,我认为版本并不重要。

【讨论】:

  • 似乎 JDBC 实现专门针对“备份”和“恢复”的小写字符串。当我尝试使用大写时,它无法正确解析。
【解决方案2】:

引号和stat.close() 无关紧要,根据我测试过的链接:`https://bitbucket.org/xerial/sqlite-jdbc/wiki/Usage'。但是,当文件路径包含空格时,引号会有所帮助。

我认为这可能是由于 jdbc 驱动程序。试试 Xerial 的 SQLite JDBC 驱动程序,它对我来说很好用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多