×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Francesco Leoni
Added: Jun 19, 2017 8:00 PM
Modified: Jun 19, 2017 8:02 PM
Views: 5
Import a pre-made SQLite DB into a CN1 app. Database file must be in the root of the src/ folder
  1. try {
  2.             String DB_NAME = "DBNAME.db";
  3.             Database myDataBase = com.codename1.db.Database.openOrCreate(DB_NAME);
  4.  
  5.             if (Storage.getInstance().readObject("initalized") == null) {
  6.                 myDataBase.close();
  7.  
  8.                 String path = com.codename1.db.Database.getDatabasePath(DB_NAME);
  9.                 OutputStream o = FileSystemStorage.getInstance().openOutputStream(path);
  10.                 InputStream i = Display.getInstance().getResourceAsStream(getClass(), "/" + DB_NAME);
  11.                 Util.copy(i, o);
  12.                 Util.cleanup(o);
  13.                 Util.cleanup(i);
  14.  
  15.                 myDataBase = com.codename1.db.Database.openOrCreate(DB_NAME);
  16.                 Storage.getInstance().writeObject("initalized", "true");
  17.             }
  18.         } catch (Exception e) {
  19.             e.printStackTrace();
  20.         }