从json android向数据库添加数据的更快方法
我有大约70000个单词、字典、同义词和反义词的列表供我的字典应用程序使用。 这些都存储在我的JSON文件中。 安装应用程序后,这些数据必须从JSON文件传输到我的本地数据库从json android向数据库添加数据的更快方法,android,android-json,android-database,Android,Android Json,Android Database,我有大约70000个单词、字典、同义词和反义词的列表供我的字典应用程序使用。 这些都存储在我的JSON文件中。 安装应用程序后,这些数据必须从JSON文件传输到我的本地数据库 try { Log.d(LOG_TAG,"parsing json"); InputStream inputStream = mContext.getAssets().open("worddictionary.js
try {
Log.d(LOG_TAG,"parsing json");
InputStream inputStream = mContext.getAssets().open("worddictionary.json");
int size = inputStream.available();
Log.d(LOG_TAG,"parsing json size: "+size);
byte[] buffer = new byte[size];
inputStream.read(buffer);
inputStream.close();
json = new String(buffer, "UTF-8");
JSONArray jsonArray = new JSONArray(json);
for (int i=0;i<jsonArray.length();i++){
notification.setProgress(jsonArray.length(),i,false);
notificationManager.notify(1,notification.build());
JSONObject obj = jsonArray.getJSONObject(i);
word = obj.getString("word");
definition = obj.getString("definition");
audiourl = obj.getString("audiourl");
synonym = obj.getString("synonym");
antonym = obj.getString("antonym");
ContentValues values = new ContentValues();
values.put("word",word);
values.put("definition",definition);
values.put("audiourl",audiourl);
values.put("synonyms",synonym);
values.put("antonyms",antonym);
long id = mDatabase.insert(WordDictionaryContract.WordDictionaryEntry.TABLE_NAME,null,values);
if (id==-1){
Log.d(LOG_TAG,"insert failed: "+word);
} else{
Log.d(LOG_TAG,"insert success id: "+id);
}
}
} catch (IOException e) {
e.printStackTrace();
Log.d(LOG_TAG,"Read json error: "+e);
} catch (JSONException e) {
Log.d(LOG_TAG,"Parse json error: "+e.getStackTrace());
e.printStackTrace();
}
试试看{
d(Log_标记,“解析json”);
InputStream InputStream=mContext.getAssets().open(“worddictionary.json”);
int size=inputStream.available();
d(Log_标记,“解析json大小:”+size);
字节[]缓冲区=新字节[大小];
inputStream.read(缓冲区);
inputStream.close();
json=新字符串(缓冲区,“UTF-8”);
JSONArray JSONArray=新JSONArray(json);
对于(int i=0;我为什么不随应用程序一起提供数据库?数据库是一个与任何其他文件一样的文件。请检查:如何将数据库与应用程序一起放置,当我们安装时,将创建新数据库并覆盖现有数据库