使用Java从磁盘备份和恢复sqlite到内存

时间:2013-07-09 12:21:24

标签: java sqlite jdbc

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

我在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数据库),this._conn已打开,但如果我想在其上执行语句,则表明内部没有表或数据。它似乎无法恢复任何东西。

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

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

2 个答案:

答案 0 :(得分:0)

看起来你错过了两件事: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的唯一方法。在这种情况下,我不认为这个版本很重要。

答案 1 :(得分:0)

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

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