2

I want to use greenDAO as sqlite DB ORM

but my database should be located on sdcard in a specific folder like /sdcard/myapp/database.sqlite

So how to choose the folder of database to be created in using greenDAO ?

MBH
  • 16,271
  • 19
  • 99
  • 149

1 Answers1

6

For quite some time (Android 2.2 I believe) it is possible use SQLiteOpenHelper with an absolute path. So for the greendao sample app (only tested with Android 4.4 and 5.0)

File path = new File(Environment.getExternalStorageDirectory(), "my_sdcard_dir/deeper_dir/notes-db");
path.getParentFile().mkdirs();

DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, path.getAbsolutePath(), null);
db = helper.getWritableDatabase();

daoMaster = new DaoMaster(db);
...

Alternatively, as the DaoMaster only needs the database itself :

File path = new File(Environment.getExternalStorageDirectory(), "my_sdcard_dir/deeper_dir/notes-db");
path.getParentFile().mkdirs();

db = SQLiteDatabase.openOrCreateDatabase(path, null);
DaoMaster.createAllTables(db, true);

daoMaster = new DaoMaster(db);
...
bwt
  • 17,292
  • 1
  • 42
  • 60
  • 1
    Thanks man, It helped me alot to solve my problem, I am planning to create a project example on Github for custom Database paths. – Naveed Ahmad Jul 26 '16 at 09:12