我最近开始学习android,所以我不知道如何处理这个问题。
我想知道如何从数据库中的may表中获取单行
我阅读了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;
}
这是我的数据库适配器代码
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();
部分有问题,所以我还没填。如您所见,我有boolean类型、integer类型和float,在androidhive示例中,所有列类型都是string,我不明白他为什么要将它们精确地解析为integer类型,以及我在代码中对这部分做了什么?
这是仙女座的一部分
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
最佳答案
sqlite没有本机布尔数据类型。请参见Datatypes doc for SQLite.您可以存储int值,如0
表示false
和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
的值等于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
}