不确定是否要保存数据

时间:2018-11-15 09:28:32

标签: java android sql database sqlite

MainData-Sqlite OpenHelper类

MainDataHelper-模式类

UsageSettings-获取数据类

主数据

保存数据

public boolean create(MainDataHelper information) {

    boolean createSuccessful = false;

    ContentValues values = new ContentValues();

    values.put(KEY_MOST_MESSAGES_SENT, information.getMostMessagesSent());
    values.put(KEY_REVIEWED, information.getReviews());

    SQLiteDatabase db = this.getWritableDatabase();

    createSuccessful = db.insert(TABLE_NAME, null, values) > 0;
    db.close();

    return createSuccessful;
}

获取数据

public Cursor getAllQuestions(FragmentActivity usageSettings) {

    return this.getWritableDatabase().query(TABLE_NAME,null,null,null,null,null,null);
}

用法设置

保存数据

        MainDataHelper information = new MainDataHelper();
    information.setMostMessagesSent("HELL");
    information.setReviews(10);

    if(new MainData(getApplicationContext()).create(information)){
        Log.d("TAGGGG", "save ok.");
    }else{
        Log.d("TAGGGG", "save failed.");
    }

获取数据

        MainData myDBHlpr = new MainData(getActivity());

        Cursor csr = myDBHlpr.getAllQuestions(getActivity());
    while (csr.moveToNext()) {

        mMostMessagesSent.setText(csr.getString(csr.getColumnIndex("MostMessagesSent")));
        mMostMessagesSent.setTextColor(Color.WHITE);
        Log.d("TAGGGGGGG",csr.getString(csr.getColumnIndex("MostMessagesSent")));
    }

错误在哪里?因为当我尝试获取它时,textview是空白的,所以我将Log.D行进行检查,并显示错误消息,提示它需要打印一条消息,这意味着 csr.getColumnIndex(“ MostMessagesSent”))为空

为什么会这样?有人可以帮帮我吗...一个星期以来一直卡住

0 个答案:

没有答案