我正在开发一个琐事应用程序,我已经完成了在应用程序中实现Room Persistence,但是我无法找到信息,我可以从我随应用程序一起提供的sqlite数据库文件中导入现有表.
解决方法:
这似乎有点晚了,但我通过这个解决方案解决了这个问题.
在初始化房间数据库之前,需要将现有的sqlite db文件复制到/ data / data / {your package name} / databases目录
您可以使用此方法来复制文件
public static voID copyDataBase(Context context, String dbname) throws IOException { inputStream myinput = context.getAssets().open(dbname); String outfilename = "/data/data/" +context.getApplicationContext().getPackagename() + "/databases/" + dbname; file file=new file(outfilename); if (file.exists()) return; OutputStream myOutput = new fileOutputStream(outfilename); byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer)) > 0) { myOutput.write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myinput.close();}
然后在你的应用程序类中执行以下 *** 作:
public class ErrorReporterInit extends Application {public static ApplicationComponent applicationComponent;@InjectdispatchingAndroIDInjector<Activity> activitydispatchingAndroIDInjector; @OverrIDepublic voID onCreate() { super.onCreate(); try { GeneralUtil.copyDataBase(this,"{your existing db file in asset}"); } catch (IOException e) { e.printstacktrace(); } applicationComponent = DaggerApplicationComponent.builder() .application(this).build(); applicationComponent.inject(this);}@OverrIDepublic voID onTerminate() { super.onTerminate();}}
在我的情况下,我使用匕首初始化房间作为依赖
总结以上是内存溢出为你收集整理的如何将现有的SQLite表导入Android Room?全部内容,希望文章能够帮你解决如何将现有的SQLite表导入Android Room?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)