具有现稀有据库和自界说文件路径的Android Sugar ORM
我完全可以或许行使提供的示例行使Sugar ORM. 在我的用例中,我从处事器下载了一个SQLite DB(ETL加载它的数百万笔记录,因此必需在处事器端完成).下载生涯到内部存储上的自界说路径. 在我的环境下,我不必要基于POCO建设动态数据库. 假如全部POCO类字段都与表布局匹配,是否可以将Sugar ORM与预先存在的SQLite DB一路行使,指向自界说路径? 办理要领>起首,我对Sugar的设法感想不惬意app类.假如我尚有其他使命必要执行怎么办? 在app开始之前?!以是让我们用本身的方法扩展SugarApp 然后,AppClass在清单中注册appClass名称.另外,这是我第一次信托init db的正确位置. public class MyAppStartClass extends SugarApp { @Override public final void onCreate() { init(); super.onCreate(); } private void init() { initDB(); } private void initDB() { try { if (!doesDatabaseExist(this,consts.dbPath)) { Context context = getApplicationContext(); SQLiteDatabase db = context.openOrCreateDatabase(consts.dbName,context.MODE_PRIVATE,null); db.close(); InputStream dbInput = getApplicationContext().getAssets().open(consts.dbName); String outFileName = consts.dbPath; OutputStream dbOutput = new FileOutputStream(outFileName); try { byte[] buffer = new byte[1024]; int length; while ((length = dbInput.read(buffer)) > 0) { dbOutput.write(buffer,length); } } finally { dbOutput.flush(); dbOutput.close(); dbInput.close(); } } } catch (Exception e) { e.toString(); } } private boolean doesDatabaseExist(ContextWrapper context,String dbName) { File dbFile = context.getDatabasePath(dbName); return dbFile.exists(); } } > Manifest:android:name =“com.myPackageName.MyAppStartClass” SQLiteDatabase db = context.openOrCreateDatabase(consts.dbName,null); db.close(); public class Book extends SugarRecord { String title; String edition; public Book(){ } public Book(String title,String edition){ this.title = title; this.edition = edition; } } 6.假如要行使现有表.请留意,Sugar会查找大写列名称,因此假如您现有的表列名称为小写,则永久不会从中获取任何现稀有据! 这让我得出了一个不甘心的结论:假如你从新开始db并行使它来为你天生db和table,那么Sugar就很棒.可是当你已经有一个包括数据的现稀有据库时,环境并非云云. (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |