我想将greenDAO用作sqlite DB ORM
但我的数据库应位于特定文件夹(如/sdcard/myapp/database.sqlite
那么如何选择使用greenDAO创建的数据库文件夹?
答案 0 :(得分:4)
很长一段时间(我相信Android 2.2)可以使用SQLiteOpenHelper
和绝对路径。所以对于greendao示例应用程序(仅使用Android 4.4和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);
...
或者,因为DaoMaster
只需要数据库本身:
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);
...