codenameone

Replace SQLite Database with External SQLite Database


I am trying to replace SQLite Database with a external SQLite Database which is selected using a FileChooser.

The aim is make the database shareable across different devices. That is when changes are made in database in a device A, this database can be used in another device B.

How can SQLite database created in AppHomePath/database folder be replaced with database selected via FileChooser

EDIT

In a simulator, the following snippet saves selected database in AppHomePath instead of AppHomePath/database

String selectedDbPath = "file:///tmp/temp11455781152272037918.db";
Util.copy(FileSystemStorage.getInstance().openInputStream(selectedDbPath),
Storage.getInstance().createOutputStream("DbName.db"));

selectedDbPath is the path returned by FileChooser

How can DbName.db be saved in AppHomePath/database directory?


Solution

  • This is discussed in the Codename One Developer Guide here:

    Some SQLite apps ship with a "ready made" database. We allow you to replace the DB file by using the code:

    String path = Display.getInstance().getDatabasePath(“databaseName”);

    You can then use the FileSystemStorage class to write the content of your DB file into the path. Notice that it must be a valid SQLite file!