Skip to content Skip to sidebar Skip to footer

Backup In Memory Sqlite Db To Byte Array Using Jooq Context

private byte[] inMemSqliteDbBackup() { byte[] data = null; try (DSLContext dsl = DSL.using('jdbc:sqlite::memory:') { ... //insert some data dsl.exe

Solution 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 databaseConnectionconn= DriverManager.getConnection("jdbc:sqlite:");
Statementstmt= 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 databaseConnectionconn= DriverManager.getConnection("jdbc:sqlite:");
// Restore the database from a backup fileStatementstat= 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:

nativesynchronizedintbackup(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

Post a Comment for "Backup In Memory Sqlite Db To Byte Array Using Jooq Context"