正在創建超過20k條目的醫學詞典。在保存此數據以供脫機使用時,由於數據庫鎖定導致應用程序崩潰。我該如何解決這個問題?執行批量插入時鎖定的Android Sqlite數據庫
我插入功能
public static void saveWords(AppCompatActivity appCompatActivity, Context context, JSONArray response) {
DbHelper helper = new DbHelper(context);
ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Finalising...");
progressDialog.setCancelable(false);
progressDialog.show();
db = helper.getWritableDatabase();
db.beginTransaction();
try {
ContentValues contentValues = new ContentValues();
for (int i = 0; i < response.length(); i++) {
JSONObject object = response.getJSONObject(i);
String wordId = object.getString("wordid");
String category = object.getString("category");
String word = object.getString("word");
String meaning = object.getString("meaning");
String letter = object.getString("letter");
if (!wordExists(context, wordId)) {
contentValues.put(DbConstants.WORD_ID, wordId);
contentValues.put(DbConstants.CATEGORY, category);
contentValues.put(DbConstants.WORD, word);
contentValues.put(DbConstants.MEANING, meaning);
contentValues.put(DbConstants.LETTER, letter);
contentValues.put(DbConstants.STATUS, "0");
db.insert(DbConstants.TABLE_WORDS, null, contentValues);
}
}
db.setTransactionSuccessful();
} catch (JSONException e) {
e.printStackTrace();
} finally {
db.endTransaction();
progressDialog.dismiss();
appCompatActivity.recreate();
}
}
wordExists方法:
private static boolean wordExists(Context context, String wordId) {
DbHelper helper = new DbHelper(context);
db = helper.getWritableDatabase();
String[] columns = {DbConstants.WORD_ID};
String whereClause = DbConstants.WORD_ID + " = ? ";
String whereArgs[] = {wordId};
Cursor cursor = db.query(DbConstants.TABLE_WORDS, columns, whereClause, whereArgs, null, null, null, null);
if (cursor.getCount() > 0) {
cursor.close();
return true;
}
cursor.close();
return false;
}
我似乎無法明白的地方問題。
完全脫離話題 –