微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在尝试将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 …)

解决方法

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

原文地址:https://www.jb51.cc/java/129093.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐