有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在sqlite 安卓中从表中获取单行

我最近开始学习安卓,所以我不知道如何处理这个问题。 我想知道如何从数据库中的一个表中获取单行 我阅读安卓hive 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;
}

这是我的数据库适配器代码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();部分有问题,所以我还没有填好。 正如你们所看到的,我有布尔型,整数型,还有浮点型,在安卓hive示例中,所有列类型都是字符串,我无法理解他为什么要将它们解析为整数型,以及我在代码中对这部分做了什么? 这是安卓hive中的角色

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

共 (1) 个答案

  1. # 1 楼答案

    SQLite没有本机的布尔数据类型。请参见Datatypes doc for SQLite.您可以为false存储0,为true存储1等int值。 试试这个

    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中,检查lunchbreakfast的值是否等于1如下所示

    if(lunch == 1) {
       //lunch is int value fetched from db it means true do what ever you want to do
    } else {
       //it means false do what ever you want to do
    }