与SQLite一起使用时,string.format不返回正确的字符串

时间:2017-06-08 21:23:11

标签: java android sqlite android-studio

所需的输出:您能将[家务]与您的一个值(例如[值1],[值2]和[值3])联系起来吗?如果是这样,活动与您的价值有何关系?

实际输出:你能将[null]与你的一个值(例如[value one],[value two]和[value 3])联系起来吗?如果是这样,活动与您的价值有何关系?你能将[chore one]与你的一个值(例如[null],[null]和[null])联系起来吗?如果是这样,活动如何与您的价值相关?

查询方法:

   public String getChoreOneWValues() {
    SQLiteDatabase db = helper.getWritableDatabase();
    //Select
    String[] columns= {StressQuizSQLiteHelper.CHORE_ONE,StressQuizSQLiteHelper.VALUE_ONE,
            StressQuizSQLiteHelper.VALUE_TWO, StressQuizSQLiteHelper.VALUE_THREE};
    Cursor cursor=db.query(StressQuizSQLiteHelper.REC_CHOICE_TABLE_NAME, columns, null, null, null, null, null);
    StringBuilder buffer=new StringBuilder();
    while(cursor.moveToNext())
    {
        int index1=cursor.getColumnIndex(StressQuizSQLiteHelper.CHORE_ONE);
        int index2=cursor.getColumnIndex(StressQuizSQLiteHelper.VALUE_ONE);
        int index3=cursor.getColumnIndex(StressQuizSQLiteHelper.VALUE_TWO);
        int index4=cursor.getColumnIndex(StressQuizSQLiteHelper.VALUE_THREE);
        String choreOne=cursor.getString((index1));
        String value1=cursor.getString((index2));
        String value2=cursor.getString((index3));
        String value3=cursor.getString((index4));
        buffer.append(String.format("Can you relate \"%s\" to one of your values (such as %s, %s and %s)?" +
                " If so, how is the activity related to your value(s)? ", choreOne, value1,value2, value3));
    }
    cursor.close();
    return buffer.toString();

}

创建表字符串:

private static final String CREATE__REC_CHOICE_TABLE = "create table "
            + REC_CHOICE_TABLE_NAME + " ("
            + REC_CHOICE_TABLE_NAME_UID
            + " integer primary key autoincrement not null,"
            + VALUE_ONE
            + " text,"
            + VALUE_TWO
            + " text,"
            + VALUE_THREE
            + " text,"
            + CHORE_ONE
            + " text,"
            + CHORE_TWO
            + " text,"
            + CHORE_THREE
            + " text,"
            + CHORE_FOUR
            + " text,"
            + CHORE_FIVE
            + " text,"
            + VALUE_JOURNAL_ENTRY_ONE
            + " text,"
            + VALUE_JOURNAL_ENTRY_TWO
            + " text,"
            + VALUE_JOURNAL_ENTRY_THREE
            + " text,"
            + VALUE_JOURNAL_ENTRY_FOUR
            + " text,"
            + VALUE_JOURNAL_ENTRY_FIVE
            + " text"
            + ");";

插入方法:

public long insertPersonalValues(String one, String two, String three){
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    contentValues.put(StressQuizSQLiteHelper.VALUE_ONE, one);
    contentValues.put(StressQuizSQLiteHelper.VALUE_TWO, two);
    contentValues.put(StressQuizSQLiteHelper.VALUE_THREE, three);

    long id = db.insert(StressQuizSQLiteHelper.REC_CHOICE_TABLE_NAME, null, contentValues);
    return id;

}

public long insertChores(String one, String two, String three, String four, String five){
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    contentValues.put(StressQuizSQLiteHelper.CHORE_ONE, one);
    contentValues.put(StressQuizSQLiteHelper.CHORE_TWO, two);
    contentValues.put(StressQuizSQLiteHelper.CHORE_THREE, three);
    contentValues.put(StressQuizSQLiteHelper.CHORE_FOUR, four);
    contentValues.put(StressQuizSQLiteHelper.CHORE_FIVE, five);

    long id = db.insert(StressQuizSQLiteHelper.REC_CHOICE_TABLE_NAME, null, contentValues);
    return id;
}

1 个答案:

答案 0 :(得分:0)

我使用update语句将空值更新为所需的值。