-1

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 !

Shivam Bhalla
  • 1,879
  • 5
  • 35
  • 63

1 Answers1

-1

You define a the following method:

public static boolean isDatabaseOpened() {
    if (myDataBase == null) {
        return false;
    }
    return myDataBase.isOpen();
}

you're using myDatabase.isOpen() and according to the documentation, it returns:

True if the database is currently open (has not been closed).

However, your insertQuestion() method does close the database.

[UPDATE]

Do not close the database at the end of the insertQuestion() method. You should rather do that on the onDestroy method. Open it once, in onCreate method and close it when you no longer need it.

Adeeb
  • 1,271
  • 3
  • 16
  • 33
  • Yes, but I am calling this method before I close the database. That method is to check whether the DB is open or not . – Shivam Bhalla Apr 30 '15 at 08:36
  • remove the `isDatabaseOpened() ` check. why would the db be open by default (on the first run, that is)? – Adeeb Apr 30 '15 at 08:37
  • Yes I had done that previously but was facing an error. Check this out http://stackoverflow.com/questions/29961755/android-sqlite-illegal-state-exception – Shivam Bhalla Apr 30 '15 at 08:40
  • I tried the edit and removed the close() function call, but I still don't get any data in the database. – Shivam Bhalla Apr 30 '15 at 08:53