Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Android Sqlite
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
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)
         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 17-Jun-12 2:37am
Edited 17-Jun-12 2:38am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I hope it will help you.
Try this Tutorial...
 
Click Here
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 1 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100