11

I'm trying to read a sqlite-File into memory for better performance, when closing my application I want to write it back to hdd.

I'm using the jdbc (3.7.2) driver in Java.

According to the docs, my Code looks like

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());
}

The file exists (and its a valid sqlite db), this._conn is open, but if I want to execute statements on it, it appears that there is no table nor data inside. It seems it doesn't restore anything.

Any suggestions on how to solve/debug that further?

(by the way - if I use stat.executeUpdate("backup to test.db")on my connection, it backups my empty :memory: db...)

3
  • The file exists, and it's a valid SQLite database - but does it contain any tables?
    – mthmulders
    Commented Jul 9, 2013 at 12:24
  • yes it does. And data as well.
    – eraelpeha
    Commented Jul 9, 2013 at 13:13
  • @mjreaper..did you find the solution for this code? can you help me how to restore?
    – Java Man
    Commented Oct 23, 2013 at 8:50

2 Answers 2

4

It looks like you are missing two things:

  1. The quotes around the file name.
  2. stat.close.

Try the following:

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();
}

That was the only way I could get it to work with Xerial SQLite JDBC version 3.7.15-M1. I do not think that the version matters much in this case.

1
  • It seems that the JDBC implementation looks specifically for lower case strings for 'backup' and 'restore'. When I try it with upper case, it doesn't parse it correctly. Commented Oct 18, 2020 at 20:32
1

The quotes and stat.close() don't matter, according to the link: `https://bitbucket.org/xerial/sqlite-jdbc/wiki/Usage', which I have tested. Quotes help when file path contains spaces, though.

I think it maybe due to the jdbc driver. Try Xerial's JDBC driver for SQLite, which works fine for me.

Not the answer you're looking for? Browse other questions tagged or ask your own question.