gpt4 book ai didi

java - 从sqlite android中的表中获取单行

转载 作者:太空狗 更新时间:2023-10-29 16:36:55 24 4
gpt4 key购买 nike

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

这是我的数据库适配器代码获取单行部分

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 类型、整数类型和 float ,在 androidhive 示例中所有列类型都是字符串,我不明白为什么他将它们准确地解析为整数类型以及我在代码中与这部分有什么关系?这是androidhive中的部分

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

最佳答案

SQLite 没有原生的 boolean 数据类型。请看Datatypes doc for SQLite.您可以存储 int 值,如 0 代表 false1 代表 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 中检查 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
}

关于java - 从sqlite android中的表中获取单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26355615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com