I am using the following code to add JSON Arrays to SQLite database :
QuestionORM Class :
public static void insertQuestion(Context c,JSONArray jarr,String search) throws JSONException {
DatabaseWrapper databaseWrapper = new DatabaseWrapper(c);
if(isDatabaseOpened())
{
myDataBase = databaseWrapper.getWritableDatabase();
ContentValues values = postToContentValues2(jarr);
values.put(QuestionORM.COLUMN_SEARCH,search);
long questionId = myDataBase.insert(QuestionORM.TABLE_NAME, "null", values);
Log.e(TAG, "Inserted new Question with ID: " + questionId);
myDataBase.close();
}
}
private static ContentValues postToContentValues2(JSONArray jsonArray) throws JSONException {
ContentValues values = new ContentValues();
for(int i=0;i<jsonArray.length();i++)
{
JSONObject job1 = jsonArray.getJSONObject(i);
if(job1!=null)
{
JSONObject job2 = job1.getJSONObject("owner");
values.put(QuestionORM.COLUMN_ID, job1.getString("question_id"));
values.put(QuestionORM.COLUMN_TITLE,job1.getString("title"));
values.put(QuestionORM.COLUMN_AUTHOR,job2.getString("display_name"));
values.put(QuestionORM.COLUMN_VOTES,job1.getString("score"));
}
}
return values;
}
public static boolean isDatabaseOpened() {
if (myDataBase == null) {
return false;
}
return myDataBase.isOpen();
}
This is used like this in another activity :
QuestionORM.insertQuestion(MainActivity.this,mJSONArr,url);
However, the log is never displayed, and the database is empty. I don't get any errors in my logcat either.
What is wrong ? How do I fix this ?
Thanks !
Aucun commentaire:
Enregistrer un commentaire