Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am trying to retrieve data from my sqlite data base table which is MealFood and its contain : date time meal_type , Food_id . when i insert data its inserted successfully and i ensure that through the log but when iam trying to retrieve the data from it the cursor returns empty

create MealFood statement
Java
private static final String MealFOOD_TABLE_CREATE= "create table IF NOT EXISTS MealFood (Date  text  not null , "+"Time   text not null,MealType  text not null,"+"Food_ID  integer   not null ,  primary key(Date,Time ,MealType,Food_ID) );" ;



retrieve method

<pre lang="java">
public Cursor  getSpecificMealFoodsID(String date,String mealType) throws SQLException
	{
		Cursor cursor=db.query(MEALFOOD_TABLE_NAME, new String[]{KEY_MFFOODID},
	KEY_MFDATE+ " = '" + date + "'"+ " "+ "and" +" "+ KEY_MFMEALTYPE+ " = '" + mealType + "'", null, null, null, null,null);
		if (cursor != null) {
			cursor.moveToFirst();
		}
		return cursor;
}



the code where i use the getSpecificMealFoodsID(String date,String mealType)
Java
     int food_id;
     int [] foods_ID;
DBAdapter dbAdapter = new DBAdapter(this);
dbAdapter.open();
String selected_date= date_btn.getText().toString();
Cursor BFMeal_foods_c=dbAdapter.getSpecificMealFoodsID(selected_date,"Breakfast");
Log.e("BFM_cursor",BFMeal_foods_c.getCount()+" ");//  it prints BFM_cursor  0

   foods_ID=new int[BFMeal_foods_c.getCount()];
    int i =0;
    if (BFMeal_foods_c.moveToFirst())
    {

        do {
             food_id= BFMeal_foods_c.getInt(0);
             Log.e("food_ID",food_id+" ");
             foods_ID[i]=food_id;
             i++;
            }

       while (BFMeal_foods_c.moveToNext());
     }


    BFMeal_foods_c.close();
    BFMeal_foods_c.deactivate();
    dbAdapter.close();


if the insert to the table is done successfully and i can retrieve data from the other tables in my data base , why when i tried to retrieve the data from this table the cursor returned empty ? is there problem in the retrieve method ? and in some times it give me an exception saying that there is " data base object" a cursor not closed but i closed the cursor and there is no cursor i was opened and didn't close it . why this exception occur ,since i closed the cursor ?please any body can help me

any help would be appreciated ...
thanks in advance
Posted
Updated 17-Jun-12 1:38am
v2

1 solution

I hope it will help you.
Try this Tutorial...

Click Here
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900