我开始编写饮食计划程序项目,这是我的数据库表。我使用外部数据库并在其中定义表外键,然后将其复制到资产文件夹中,然后将其连接到我的项目。
standardUnit,Foods和standardFoodUnit是3个具有静态数据的表,我之前已填充它们,但是EatenFood表在计算后被动态填充。
 我使用模型类并尝试使用androidhive database tutorial指令编写databaseAdapter。但是因为我最近启动了android,所以我对此没有任何见识。
尝试阅读书籍或在线教程,但他们让我更加困惑。现在这是我的问题,我想为EatenFood表外键知道如何放置food-id值?我之前在数据库中定义了food_id INTEGER REFERENCES Foods ( _id ),但是在databaseAdapter类中用于插入或更新或获取功能,我不知道该怎么办使用此外键行事。



这是EatenFood桌子的模型课

public class EatenFood {

int eatenfoodid;
boolean breakfast;
boolean lunch;
boolean snack;
boolean appetizers;
boolean dinner;
Data day;
String equivalent;
boolean dairy;
boolean vegetables;
boolean fruit;
boolean meat_bean_egg;
boolean bread_cereals;
boolean fat;
boolean suger;
double unitsum;
int food_id;

public boolean isAppetizers() {
    return appetizers;
}
public void setAppetizers(boolean appetizers) {
    this.appetizers = appetizers;
}
public Data getDay() {
    return day;
}
public void setDay(Data day) {
    this.day = day;
}
public double getUnitsum() {
    return unitsum;
}
public void setUnitsum(double unitsum) {
    this.unitsum = unitsum;
}
public int getFood_id() {
    return food_id;
}
public void setFood_id(int food_id) {
    this.food_id = food_id;
}
//all remaining getter and setter .........}


餐桌模型类

public class Foods {
int foodid;
String foodname;
boolean breakfast;
boolean lunch;
boolean snack;
boolean appetizers;
boolean dinner;
boolean mainfood;
boolean secondary;


public boolean isAppetizers() {
    return appetizers;
}
public void setAppetizers(boolean appetizers) {
    this.appetizers = appetizers;
}
public int getFoodid() {
    return foodid;
}
public void setFoodid(int foodid) {
    this.foodid = foodid;
}
//all remaining getter and setter .........}


DatabaseAdapter函数

public class DatabaseAdapter {

private final String TAG = "DatabaseAdapter";
private DatabaseOpenHelper openHelper;
public Long insertEatenFood(EatenFood eatenfood) {
    SQLiteDatabase myDataBase = null;
    Long id = -1L;
    try {
        ContentValues values = new ContentValues();
        values.put(TABLE_EATENFOOD_BREAKFAST, eatenfood.isBreakfast());
        values.put(TABLE_EATENFOOD_LUNCH, eatenfood.isLunch());
        values.put(TABLE_EATENFOOD_SNACK, eatenfood.isSnack());
        values.put(TABLE_EATENFOOD_APPETIZERS, eatenfood.isAppetizers());
        values.put(TABLE_EATENFOOD_DINNER, eatenfood.isDinner());
        // values.put(TABLE_EATENFOOD_DATA, eatenfood.getDay().getClass());
        values.put(TABLE_EATENFOOD_EQUIVALENT, eatenfood.getEquivalent());
        values.put(TABLE_EATENFOOD_DAIRY, eatenfood.isDairy());
        values.put(TABLE_EATENFOOD_VEGETABLES, eatenfood.isVegetables());
        values.put(TABLE_EATENFOOD_FRUIT, eatenfood.isFruit());
        values.put(TABLE_EATENFOOD_MEAT_BEAN_EGG,
                eatenfood.isMeat_bean_egg());
        values.put(TABLE_EATENFOOD_BREAD_CEREALS,
                eatenfood.isBread_cereals());
        values.put(TABLE_EATENFOOD_FAT, eatenfood.isFat());
        values.put(TABLE_EATENFOOD_SUGER, eatenfood.isSuger());
        values.put(TABLE_EATENFOOD_UNITSUM, eatenfood.getUnitsum());

        myDataBase = openHelper.getWritableDatabase();
        id = myDataBase.insert(TABLE_EATENFOOD, null, values);

    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    } finally {
        if (myDataBase != null && myDataBase.isOpen())
            myDataBase.close();
    }
    return id;
}

// update EateanFood table =====================================================
public int updateEatenFood(EatenFood eatenfood) {
    SQLiteDatabase myDataBase = null;
    int count = -1;
    try {
        myDataBase = openHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(TABLE_EATENFOOD_BREAKFAST, eatenfood.isBreakfast());
        values.put(TABLE_EATENFOOD_LUNCH, eatenfood.isLunch());
        values.put(TABLE_EATENFOOD_SNACK, eatenfood.isSnack());
        values.put(TABLE_EATENFOOD_APPETIZERS, eatenfood.isAppetizers());
        values.put(TABLE_EATENFOOD_DINNER, eatenfood.isDinner());
        // values.put(TABLE_EATENFOOD_DATA, eatenfood.getDay().getClass());
        values.put(TABLE_EATENFOOD_EQUIVALENT, eatenfood.getEquivalent());
        values.put(TABLE_EATENFOOD_DAIRY, eatenfood.isDairy());
        values.put(TABLE_EATENFOOD_VEGETABLES, eatenfood.isVegetables());
        values.put(TABLE_EATENFOOD_FRUIT, eatenfood.isFruit());
        values.put(TABLE_EATENFOOD_MEAT_BEAN_EGG,
                eatenfood.isMeat_bean_egg());
        values.put(TABLE_EATENFOOD_BREAD_CEREALS,
                eatenfood.isBread_cereals());
        values.put(TABLE_EATENFOOD_FAT, eatenfood.isFat());
        values.put(TABLE_EATENFOOD_SUGER, eatenfood.isSuger());
        values.put(TABLE_EATENFOOD_UNITSUM, eatenfood.getUnitsum());

        count = myDataBase
                .update(TABLE_EATENFOOD, values, TABLE_EATENFOOD_ID + "=?",
                        new String[] { String.valueOf(eatenfood
                                .getEatenfoodid()) });
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    } finally {
        myDataBase.close();
    }
    return count;
}

//  Getting All EatenFood ================================================

public ArrayList<EatenFood> getEatenfoods() {

    ArrayList<EatenFood> result = null;
    SQLiteDatabase myDataBase = null;
    Cursor cursor = null;
    try {
        myDataBase = openHelper.getWritableDatabase();
        cursor = myDataBase.query(TABLE_EATENFOOD, new String[] { "*" }, null, null,
                null, null, null);
        if (cursor.moveToFirst()) {
            result = new ArrayList<EatenFood>();
            do {
                result.add(extractEatenFood(cursor));
            } while (cursor.moveToNext());
        }
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
    finally {
        if (cursor != null) {
            cursor.close();
        }
        myDataBase.close();
    }
    return result;
}

// extractEatenFood=============================================================
private EatenFood extractEatenFood(Cursor cursor){
    EatenFood eatenfood = new EatenFood();
    eatenfood.setEatenfoodid(cursor.getInt(cursor.getColumnIndex(TABLE_EATENFOOD_ID)));
    eatenfood.setBreakfast(cursor.getInt(cursor.getColumnIndex(TABLE_EATENFOOD_BREAKFAST)) != 0);
    eatenfood.setLunch(cursor.getInt(cursor.getColumnIndex(TABLE_EATENFOOD_LUNCH))!=0);
    eatenfood.setSnack(cursor.getInt(cursor.getColumnIndex(TABLE_EATENFOOD_SNACK))!=0);
    eatenfood.setAppetizers(cursor.getInt(cursor.getColumnIndex(TABLE_EATENFOOD_APPETIZERS))!=0);
    eatenfood.setDinner(cursor.getInt(cursor.getColumnIndex(TABLE_EATENFOOD_DINNER))!=0);
    //      ???????????????????????? baraye day k sabt beshe
    eatenfood.setEquivalent(cursor.getString(cursor.getColumnIndex(TABLE_EATENFOOD_EQUIVALENT)));
    eatenfood.setDairy(cursor.getInt(cursor.getColumnIndex(TABLE_EATENFOOD_DAIRY))!=0);
    eatenfood.setVegetables(cursor.getInt(cursor.getColumnIndex(TABLE_EATENFOOD_VEGETABLES))!=0);
    eatenfood.setFruit(cursor.getInt(cursor.getColumnIndex(TABLE_EATENFOOD_FRUIT))!=0);
    eatenfood.setBread_cereals(cursor.getInt(cursor.getColumnIndex(TABLE_EATENFOOD_BREAD_CEREALS))!=0);
    eatenfood.setFat(cursor.getInt(cursor.getColumnIndex(TABLE_EATENFOOD_FAT))!=0);
    eatenfood.setSuger(cursor.getInt(cursor.getColumnIndex(TABLE_EATENFOOD_SUGER))!=0);
    eatenfood.setFood_id(cursor.getInt(cursor.getColumnIndex(TABLE_EATENFOOD_F_FOODID)));


    return eatenfood ;
}

最佳答案

每当您要将食物添加到eatenfood表中时。您必须在特定食物对象上调用getFoodid函数并获取food_id,然后在insertEatenFood类中使用DatabaseAdapter函数将其插入数据库。
最好是提及自己是问题的完整例子,这样可以更轻松地为您提供帮助。
也许您有一个关于如何找到要插入到eatenfood表中的food_id的问题。最好写出自己是算法的算法,然后找出不同用户所需的food_id

关于java - 动态表中已初始化的外键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26448300/

10-10 20:11