2013-07-09 41 views
6

我想读取一个sqlite文件到内存中以获得更好的性能,当关闭我的应用程序时我想把它写回硬盘。备份和恢复SQLite从磁盘到Java中的内存

我在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"),它备份我的空:内存:DB ...)

+0

该文件存在,它是一个有效的SQLite数据库 - 但它包含任何表? – mthmulders

+0

是的,它的确如此。还有数据。 – RalphP

+0

@ mjreaper..did你找到这个代码的解决方案?你能帮助我如何恢复? –

回答

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的唯一方法。在这种情况下,我认为版本并不重要。

0

的报价和stat.close()并不重要,根据链接:`https://bitbucket.org/xerial/sqlite-jdbc/wiki/Usage”,这我已经测试。不过,当文件路径包含空格时,引号会有所帮助。

我想这可能是由于jdbc驱动程序造成的。尝试Xerial的SQLite的JDBC驱动程序,这对我来说工作正常。