[英]getting single row from table in sqlite android
我最近开始学习android,所以我对如何处理这个问题一无所知。 我想知道如何从数据库的may表之一中获取单行,我阅读了androidhive数据库教程并尝试编写示例代码之类的代码,但是当我要编写“获取单行”部分时,我遇到了一些问题。
这是我的模特班
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();
中UserMealUnit mealunit=new UserMealUnit();
问题UserMealUnit mealunit=new UserMealUnit();
部分,所以我还没有填写。 如您所见,我具有布尔类型,整数类型和浮点型,在androidhive示例中,所有列类型都是字符串,我不明白为什么他将它们准确地解析为整数类型以及我在代码中与这部分有什么关系? 这是androidhive中的一部分
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
SQLite没有本地布尔数据类型 。 请参阅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
的价值等于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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.