从sqlite android中的表中获取单行

时间:2014-10-14 08:01:49

标签: java android database sqlite boolean

我最近开始学习android,所以我不知道如何处理这个问题。 我想知道如何从数据库中的某个表中获取单行 我读了androidhive database tutorial并尝试编写我的代码,例如示例代码,但是当我想写“获取单行”部分时,我面临一些问题。

这是我的模特课

public class UserMealUnit {
 int mealid;
 boolean breakfast;
 boolean lunch;
 float vegetables;
 public int getMealid() {
    return mealid;
}
public void setMealid(int mealid) {
    this.mealid = mealid;
}
public boolean isBreakfast() {
    return breakfast;
}
public void setBreakfast(boolean breakfast) {
    this.breakfast = breakfast;
}
public boolean isLunch() {
    return lunch;
}
public void setLunch(boolean lunch) {
    this.lunch = lunch;
}
    public float getVegetables() {
    return vegetables;
}
public void setVegetables(float vegetables) {
    this.vegetables = vegetables;
}

这是我的databaseAdapter代码getting single row部分

UserMealUnit getusermealunit(int mealid){
        SQLiteDatabase myDataBase = null;
        myDataBase = openHelper.getWritableDatabase();
        Cursor cursor=myDataBase.query(TABLE_USERMEALUNIT, new String[] {TABLE_USERMEALUNIT_ID,
                TABLE_USERMEALUNIT_BREAKFAST,
                TABLE_USERMEALUNIT_LUNCH,
                TABLE_USERMEALUNIT_VEGETABLES,

                }, TABLE_USERMEALUNIT_ID + "=?",  new String[] { String.valueOf(mealid) },
                 null, null, null, null);
         if (cursor != null)
                cursor.moveToFirst();            
         UserMealUnit mealunit=new UserMealUnit(); 

        return mealunit;


    }

我在UserMealUnit mealunit=new UserMealUnit();部分遇到问题,所以我还没有填写。 你可以看到我有布尔类型,整数类型和浮点数,在androidhive示例中所有列类型都是字符串,我无法得到为什么他将它们完全解析为整数类型以及我在代码中与此部分有什么关系? 这是androidhive中的一部分

 Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2));

1 个答案:

答案 0 :(得分:5)

SQLite没有原生布尔数据类型。请参阅Datatypes doc for SQLite.您可以为[{1}}和0存储false的{​​{1}}等int值。 试试这个

1

模型类

true

查询创建public UserMealUnit getusermealunit(int mealid) { SQLiteDatabase db = this.getReadableDatabase(); String selectQuery = "SELECT * FROM " + TABLE_USERMEALUNIT + " WHERE " + TABLE_USERMEALUNIT_ID + " = " + mealid; Log.d(LOG, selectQuery); Cursor c = db.rawQuery(selectQuery, null); if (c != null) c.moveToFirst(); UserMealUnit mealunit = new UserMealUnit(); mealunit.setMealid(c.getInt(c.getColumnIndex(KEY_ID)));//KEY_ID key for fetching id mealunit.setBreakfast((c.getInt(c.getColumnIndex(KEY_BREAKFAST))));//KEY_BREAKFAST key for fetching isBreakfast mealunit.setLunch((c.getInt(c.getColumnIndex(KEY_LUNCH))));//KEY_LUNCH key for fetching isLunch mealunit.setVegetables((c.getFloat(c.getColumnIndex(KEY_VEGETABLE))));//KEY_VEGETABLE key for fetching vegetables return mealunit; }

public class UserMealUnit {

    int mealid;
    int breakfast;
    int lunch;
    float vegetables;

    public int getMealid() {
        return mealid;
    }
    public void setMealid(int mealid) {
        this.mealid = mealid;
    }
    public int getBreakfast() {
        return breakfast;
    }
    public void setBreakfast(int breakfast) {
        this.breakfast = breakfast;
    }
    public int getLunch() {
        return lunch;
    }
    public void setLunch(int lunch) {
        this.lunch = lunch;
    }
    public float getVegetables() {
        return vegetables;
    }
    public void setVegetables(float vegetables) {
        this.vegetables = vegetables;
    }
}

TABLE_USERMEALUNIT中检查private static final String CREATE_TABLE_USERMEALUNIT = "CREATE TABLE " + TABLE_USERMEALUNIT + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_BREAKFAST + " INTEGER,"+ KEY_LUNCH + " INTEGER," + KEY_VEGETABLE + " REAL" + ")"; Activity的值是否等于此lunch

breakfast