机器人的SQLite数据没有被加 [英] Android SQLite data is not being added

查看:163
本文介绍了机器人的SQLite数据没有被加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的code以JSON阵列添加到SQLite数据库:

I am using the following code to add JSON Arrays to SQLite database :

QuestionORM类:

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);

但是,日志永远不会显示,并且该数据库是空的。我没有得到我的logcat中的任何错误,无论是。

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 ?

谢谢!

推荐答案

您定义下面的方法:

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

您正在使用 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).

不过,你的 insertQuestion()方法不关闭数据库。

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

[更新]

不要在 insertQuestion()方法的最后关闭数据库。你还是做对的onDestroy 方法。打开它一次,在的onCreate 方法,并关闭它,当你不再需要它。

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.

这篇关于机器人的SQLite数据没有被加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆