Search code examples
sqlitespring-jdbcin-memory-database

how can I change in memory db to file db In JAVA?


The sqlite configuration in spring boot is

sqlite.datasource.url=jdbc:sqlite::memory:

I confirmed that the data was inserted, but I want to get this sqlite as a real file.

Do you know what to do?


Solution

  • .properties setting

    spring.datasource.url=jdbc:sqlite:file:memorydb.db?mode=memory&cache=shared
    

    test code

     Connection conn = DriverManager.getConnection("jdbc:sqlite:file:memorydb.db?mode=memory&cache=shared");
     Statement stmt = conn.createStatement();
    
     File tmpFile = File.createTempFile(name, ".db");
    
     stmt.executeUpdate("backup to " + tmpFile.getAbsolutePath());
    
    
     tmpFile.deleteOnExit();
     stmt.close();