Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate-SQLite Dump and Import via Java

How can I make use of Hibernate to perform a backup on my sqlite database? The output should ideally be in the form of an SQL script, similar to the .dump sqlite utility.

Also, to perform a restore of the sql script programmatically as well.

like image 568
louis xie Avatar asked Sep 12 '26 06:09

louis xie


1 Answers

I have found an alternative to do this, and it is by invoking the sqlite3 command line shell via Java. Here's my sample solution

public class Test {
  public static void main(String args[]) {
    try {
      String line;
      Process p = Runtime.getRuntime().exec("cmd /c start /b sqlite3 db.sqlite .dump > dump.txt");
      BufferedReader bri = new BufferedReader
        (new InputStreamReader(p.getInputStream()));
      BufferedReader bre = new BufferedReader
        (new InputStreamReader(p.getErrorStream()));
      while ((line = bri.readLine()) != null) {
        System.out.println(line);
      }
      bri.close();
      while ((line = bre.readLine()) != null) {
        System.out.println(line);
      }
      bre.close();
      p.waitFor();
      System.out.println("Done.");
    }
    catch (Exception err) {
      err.printStackTrace();
    }
  }
}
like image 179
louis xie Avatar answered Sep 13 '26 20:09

louis xie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!