getting single row from table in sqlite android
There is no native boolean data type for SQLite. Please see Datatypes doc for SQLite. You can store int values like 0
for false
and 1
for true
.
Try like this
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_USERMEALUNIT
table
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 Activity
check the value of lunch
or breakfast
is equals to 1
like this
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
}
mkafiyan
Updated on June 04, 2022Comments
-
mkafiyan almost 2 years
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 tutorial and try to write my code like sample code,but when I want to write 'getting single row' part I face with some problem.
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 row
partUserMealUnit 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 androidhiveContact contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2));
-
mkafiyan over 9 yearsbecause of boolean type It gives me error what if I add !=0
mealunit.setBreakfast((c.getInt(c.getColumnIndex(KEY_BREAKFAST)))!=0);
? -
mkafiyan over 9 yearsThere is no boolean type in SQLite database. Instead,I must save 0 as false and 1 as true. Thus to read your value so here KEY_BREAKFAST is boolean but we have getInt which get int type I use !=0 to solve this.
-
Kaushik over 9 years@mimi : yes, post create table query for
TABLE_USERMEALUNIT
-
Kaushik over 9 years@mimi check the edited answer thats what I mean to say
-
mkafiyan over 9 yearsLet us continue this discussion in chat.