mercredi 8 avril 2015

Pull values from table and add it to a temp variable

This method is suppose to repeating until enough calories have been added to the meal plan. The getMeat method adds one value with the ftype = meat. It should then sum every value in the calorie column and set cals to that number. This should continue until cals > Calories.



int Calories = 800;

public void getMeat(String mTables, int cals) {
SQLiteDatabase db = getReadableDatabase();
Random r = new Random();
int mRand = r.nextInt(31 - 21) + 21;
db.execSQL("INSERT INTO " + mTables + " (fName, Calories, Protein, Carbs) SELECT fName, Calories, Protein, Carbs FROM Schedule WHERE ScheduleID = " + mRand );
Cursor c = db.rawQuery("SELECT SUM(Calories) FROM " + mTables, null);
c.moveToFirst();
String Caltemp = c.getString(c.getColumnIndex("SUM(Calories)"));
cals = Integer.parseInt(Caltemp);
}

public Cursor getMeal_1_day1() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
int cals = 0;
String [] sqlProject = {"0 _id", "fName", "Calories", "Protein", "Carbs"};
String sqlTables = "fstMeal_1";
Cursor mCursor = db.rawQuery("SELECT * FROM " + sqlTables, null);
if(mCursor.getCount() == 0) {
while(cals < Calories) {
getMeat(sqlTables, cals);
//getFruit(sqlTables);
//getVegetable(sqlTables);
//getDairy(sqlTables);
//getNut(sqlTables);
}
}
else {}
qb.setTables(sqlTables);
Cursor c = qb.query(db, sqlProject, null, null,
null, null, null);
c.moveToFirst();
return c;
}


The app continues to run until i get a message that the app is no longer responding so i assume that the c.getString is not working and the calories are not being read in, but im not sure as to why.


Aucun commentaire:

Enregistrer un commentaire