如何使用bean将值插入sqlite数据库并将这些表值检索到json数组中

时间:2016-08-12 06:59:23

标签: java android json sqlite

使用bean将值插入表

public static void addGetAssessmentDetail(Context context,
        GetAssessmentBean getassessmentDetail) {
    DBHelper dbHelper = null;
    SQLiteDatabase sqlDBRead = null;
    SQLiteDatabase sqlDBWrite = null;
    try {

        dbHelper = new DBHelper(context, LektzDB.DB_NAME, null,
                LektzDB.DB_VERSION);
        sqlDBRead = dbHelper.getReadableDatabase();
        sqlDBWrite = dbHelper.getWritableDatabase();

        ContentValues book = new ContentValues();       
        book.put(TB_FinalAssessmentValues.CL_1_ID, getassessmentDetail.getId());
        book.put(TB_FinalAssessmentValues.CL_2_USER_ID , getassessmentDetail.getUser_id());
        book.put(TB_FinalAssessmentValues.CL_3_BOOK_ID, getassessmentDetail.getBook_id());
        book.put(TB_FinalAssessmentValues.CL_4_CHAPTER_ID,
                getassessmentDetail.getChapter_id());
        book.put(TB_FinalAssessmentValues.CL_5_QUESTION_TYPE,
                getassessmentDetail.getQuestion_type());
        book.put(TB_FinalAssessmentValues.CL_6_QUESTION_ID,
                getassessmentDetail.getQuestion_id());
        book.put(TB_FinalAssessmentValues.CL_7_OPTION_ID,
                getassessmentDetail.getOption_id());
        book.put(TB_FinalAssessmentValues.CL_8_MARK,
                getassessmentDetail.getMark());
        book.put(TB_FinalAssessmentValues.CL_9_NOTES,
                getassessmentDetail.getNotes());
        book.put(TB_FinalAssessmentValues.CL_10_MATCH_OPTION, getassessmentDetail.getMatchOption());
        book.put(TB_FinalAssessmentValues.CL_11_DRAG_VALUES,
                getassessmentDetail.getDragValues());
        book.put(TB_FinalAssessmentValues.CL_12_ADDED_TIME,
                getassessmentDetail.getAdded_time());       
        Log.i("", "assessment values insertion success" );


    } catch (Exception e) {
        e.printStackTrace();
    }

}

尝试将这些表值检索到JSON数组

public JSONArray getFullAssessmentData(Context mContext, String bookid,
        int UserId) {
    DBHelper dbh = new DBHelper(mContext, LektzDB.DB_NAME, null,
            LektzDB.DB_VERSION);
    SQLiteDatabase db = dbh.getReadableDatabase();
    JSONArray resultSet = new JSONArray();
    try {
        Cursor c = db.rawQuery("SELECT * FROM " + TB_FinalAssessmentValues.NAME
                + " WHERE " + TB_FinalAssessmentValues.CL_3_BOOK_ID+ "='"+ bookid + "'", null);
        Log.i("tag", "msg vachindi");

        if (c.getCount() > 0) {

            c.moveToFirst();
            do {
                c.moveToFirst();
                while (c.isAfterLast() == false) {
                    int totalColumn = c.getColumnCount();
                    JSONObject rowObject = new JSONObject();   
                    for (int i = 0; i < totalColumn; i++) {
                        if (c.getColumnName(i) != null) {
                            try {
                                rowObject.put(c.getColumnName(i),
                                        c.getString(i));
                            } catch (Exception e) {
                            }
                        }
                    }
                    resultSet.put(rowObject);
                    c.moveToNext();
                }

                c.close();
                db.close();
                    }
             while (c.moveToNext());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resultSet;
}

最后尝试将这些值存储到JSON数组

JSONArray fullassessmentjson =  rdb.getFullAssessmentData( getContext(), BookId, UserId);
                    Log.i("Tag123456","Finalcheck"+fullassessmentjson);

DBHelper

db.execSQL("CREATE TABLE IF NOT EXISTS " + TB_FinalAssessmentValues.NAME + "(" 
                + TB_FinalAssessmentValues.CL_1_ID + " TEXT, "
                + TB_FinalAssessmentValues.CL_2_USER_ID  + " TEXT, "
                + TB_FinalAssessmentValues.CL_3_BOOK_ID + " TEXT, "
                + TB_FinalAssessmentValues.CL_4_CHAPTER_ID + " TEXT, "
                + TB_FinalAssessmentValues.CL_5_QUESTION_TYPE + " TEXT, "
                + TB_FinalAssessmentValues.CL_6_QUESTION_ID + " TEXT, "
                + TB_FinalAssessmentValues.CL_7_OPTION_ID + " TEXT, "
                + TB_FinalAssessmentValues.CL_8_MARK + " TEXT, "
                + TB_FinalAssessmentValues.CL_9_NOTES + " TEXT, "
                + TB_FinalAssessmentValues.CL_10_MATCH_OPTION + " TEXT, "
                + TB_FinalAssessmentValues.CL_11_DRAG_VALUES + " TEXT, "
                + TB_FinalAssessmentValues.CL_12_ADDED_TIME + " TEXT)");

,错误是它在Json数组中没有显示

1 个答案:

答案 0 :(得分:1)

似乎您正在将值添加到ContentValues对象中,但不会在数据库中执行实际插入。

(因为这个你基本上是在查询空表)

您应该在insert()末尾调用SQLiteDatabase addGetAssessmentDetail()方法,将数据插入到您的表格中:

sqlDBWrite.insert(TABLE_TO_INSERT, null, book);