java 从sqlite android中的表中获取单行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26355615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting single row from table in sqlite android
提问by mkafiyan
I start to learn android recently so I don't have much idea how can handle this problem. I want to know how can I get single row from one of may table in database I read androidhive database tutorialand try to write my code like sample code,but when I want to write 'getting single row' part I face with some problem.
我最近开始学习android,所以我不太知道如何处理这个问题。我想知道如何从数据库中的一张表中获取单行我阅读了androidhive 数据库教程并尝试像示例代码一样编写我的代码,但是当我想编写“获取单行”部分时,我遇到了一些问题。
this is my model class
这是我的模型课
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;
}
this is my databaseAdapter codes getting single rowpart
这是我的 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;
}
I have problem in UserMealUnit mealunit=new UserMealUnit();part so I don't fill it yet.
as you can see I have boolean type, integer type and also float,in androidhive sample all column type is string,I cant get why he exactly parse them to integer type and what I have to do with this part in my code?
this is the part in androidhive
我有UserMealUnit mealunit=new UserMealUnit();部分问题,所以我还没有填写。正如您所看到的,我有布尔类型、整数类型和浮点数,在 androidhive 示例中,所有列类型都是字符串,我不明白他为什么将它们准确地解析为整数类型以及我在代码中与这部分有什么关系?这是 androidhive 中的部分
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
回答by Kaushik
There is no native boolean data typefor SQLite. Please see Datatypes doc for SQLite.You can store int values like 0for falseand 1for true.
Try like this
SQLite没有原生布尔数据类型。请参阅SQLite 的数据类型文档。您可以存储 int 值,例如0forfalse和1for 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;
}
Model class
模型类
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;
}
}
Query for creating TABLE_USERMEALUNITtable
创建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" + ")";
In Activitycheck the value of lunchor breakfastis equals to 1like this
在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
}

