[英]Can I use a pre-build SQLite Database from assets file with room database library?
我正在構建一個啟動器應用程序。 在該應用程序中,一些應用程序(包名稱、圖標)將預先添加到 SQLite 中,以后用戶可以在 SQLite 數據庫中添加更多應用程序(包名稱、圖標),並實時更新數據。 我想使用房間數據庫。
所以,我的問題是,我可以使用帶有房間數據庫庫的資產文件中的預構建 SQLite 數據庫嗎?
請幫我。
您可以將 json 文件與應用程序捆綁在一起。 然后像這樣讀
try {
InputStream inputStream = mContext.getAssets().open(mContext.getPackageName() + ".json");
int size = inputStream.available();
byte[] buffer = new byte[size];
if (inputStream.read(buffer) != size) {
throw new IndexOutOfBoundsException();
}
inputStream.close();
JSONObject jsonObject = new JSONObject(new String(buffer, StandardCharsets.UTF_8));
List<SomeClass> classList = new ArrayList<>();
JSONArray jsonElements = jsonObject.getJSONArray("dataset1");
for(int i =0; i < jsonElements.length(); i++){
SomeClass someClass = new SomeClass(jsonElements.get(i));
classList.add(someClass);
}
appDatabase.insert(classList);
} catch(Exception exception){
}
那將是手動的方式。 但是,您可以通過以下方式構建備份/恢復並僅使用恢復端: 備份/恢復房間數據庫
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.