1
private byte[] inMemSqliteDbBackup() {
    byte[] data = null;
    try (DSLContext dsl = DSL.using("jdbc:sqlite::memory:") {
        ...
        //insert some data 
        dsl.execute("backup to " + data); // backup to byte[], but unsupported , a file is needed 
        dsl.connection(connection -> {
            // or, get underlying connection and open inputstream 
            // but there is no getInputStream in SqliteConnection
        });
    }
    return data;
}

How can we backup in memory sqlite db to byte[] ? I went through SqliteConnection and it does not give an InputStream either.

one option is to not use in memory db by using url as "jdbc:sqlite:/some-location" and then we can use FileUtils.readFileToByteArray(new File(some-location)) , but not sure if we can do the same with in memory sqlite db and still using DSLContext by Jooq.

1 Answer 1

1

That backup to syntax is not a native SQLite SQL syntax, but offered by the Xerial JDBC driver according to their docs here:

Take a backup of the whole database to backup.db file:

// Create a memory database
Connection conn = DriverManager.getConnection("jdbc:sqlite:");
Statement stmt = conn.createStatement();
// Do some updates
stmt.executeUpdate("create table sample(id, name)");
stmt.executeUpdate("insert into sample values(1, \"leo\")");
stmt.executeUpdate("insert into sample values(2, \"yui\")");
// Dump the database contents to a file
stmt.executeUpdate("backup to backup.db");
Restore the database from a backup file:
// Create a memory database
Connection conn = DriverManager.getConnection("jdbc:sqlite:");
// Restore the database from a backup file
Statement stat = conn.createStatement();
stat.executeUpdate("restore from backup.db");

If you reverse engineer their sources, you can see that the command is intercepted, and translated to this particular method in org.sqlite.core.NativeDB:

native synchronized int backup(byte[] dbNameUtf8, byte[] destFileNameUtf8,
        ProgressObserver observer) throws SQLException;

I.e. it is bound to the SQLite backup API, which can operate only with actual files, not with in-memory data structures.

So, I'm afraid you cannot, with the current versions of SQLite, intercept that backup and send that into a byte[] variable, without an intermediate temporary file being written, regardless if using jOOQ or JDBC or native SQLite directly

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.