In my application i am using sqlit data base for storing user information , and these information stored successfully , but they are stored for 2 days since i can retrieve them during 2 days and show them on the screen but after 2 days or more when i ran my app there is no data displayed ” the returned cursor is empty when i debug my code ” i used the DataBaseAdapter and DatabaseHelper classes for creating the data base ,i am really wondering from that , why this problem occur ? how i can solve it ?
i believe that there is no problem in the code since i can insert and retrieve the data and i found my data base in data/data/my pavkage name /databases/ DBname
any body have an idea about this issue ? please help me ……
this my code for creating the data base
public class DBAdapter
{
public static final String DB_NAME="MYDB";
private static final int DB_VERSION=5;
private static final String TRACKER_TABLE_NAME="Tracker";
private static final String EXERCISE_TABLE_NAME="Exercise";
private static final String MEAL_TABLE_NAME="Meal";
private static final String FOOD_TABLE_NAME="Food";
private static final String MEALFOOD_TABLE_NAME="MealFood";
private static final String TAG = "DBAdapter";
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
private Context context;
// table Exercise columns name
public static final String KEY_DATE="Date";
public static final String KEY_TIME="Time";
public static final String KEY_NAME="Name";
public static final String KEY_PERIOD="Period";
public static final String KEY_BURNEDCALS="Burned_Calories";
// table Tracker columns Name
public static final String KEY_TDATE="Date";
public static final String KEY_DAILY_BCALS_COUNTER="DialyBCalsCounter";
public static final String KEY_DAILY_NCALS_COUNTER="DialyNCalsCounter";
public static final String KEY_VB12_COUNTER="VB12Counter";
public static final String KEY_PROTEIN_COUNTER="ProteinCounter";
public static final String KEY_SODIUM_COUNTER="SodiumCounter";
public static final String KEY_IRON_COUNTER="IronCounter";
public static final String KEY_CHOLESTEROL_COUNTER="CholesterolCounter";
public static final String KEY_FAT_SAT_COUNTER="FatSatCounter";
public static final String KEY_FAT_MONO_COUNTER="FatMonoCounter";
// table Meal columns Name
public static final String KEY_MDATE="Date";
public static final String KEY_MTIME="Time";
public static final String KEY_MEALTYPE="MealType";
// table Food columns Name
public static final String KEY_FOODID="Food_ID";
public static final String KEY_FOODNAME="Food_Name";
public static final String KEY_CALORIES="Calories";
public static final String KEY_VB12="VB12";
public static final String KEY_CHOLESTEROL="Cholesterol";
public static final String KEY_PROTEIN="Protein";
public static final String KEY_IRON="Iron";
public static final String KEY_SODIUM="Sodium";
public static final String KEY_FAT_MONO="Fat_Mono";
public static final String KEY_FAT_Sat="Fat_Sat";
public static final String KEY_CARBOHYDRATE="carbohydrate";
// table MealFood columns Name
public static final String KEY_MFDATE="Date";
public static final String KEY_MFTIME="Time";
public static final String KEY_MFMEALTYPE="MealType";
public static final String KEY_MFFOODID="Food_ID";
private static final String EXERCISE_TABLE_CREATE ="create table Exercise (Date text not null , "+
"Time text not null ,Name text not null," + " Period REAL not null, Burned_Calories REAL not null," +
" primary key(Date,Time ) );" ;
private static final String Meal_TABLE_CREATE= "create table IF NOT EXISTS Meal (Date text not null , "+
"Time text not null,MealType text not null,"+ " primary key(Date,Time ,MealType) );" ;
private static final String FOOD_TABLE_CREATE= "create table IF NOT EXISTS Food (Food_ID INTEGER primary key AUTOINCREMENT , "+
"Food_Name text not null,Calories integer not null,"+ "VB12 integer not null,Cholesterol integer not null,"+
"Protein integer not null,Iron integer not null,Sodium integer not null,Fat_Mono integer not null,Fat_Sat integer not null,carbohydrate integer not null);" ;
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) );" ;
private static final String TRACKER_TABLE_CREATE= "create table IF NOT EXISTS Tracker (Date text not null primary key , "+
"DialyBCalsCounter integer not null,DialyNCalsCounter integer not null,"+
"VB12Counter integer not null,ProteinCounter integer not null,"+ "SodiumCounter integer not null,IronCounter integer not null,"+
"CholesterolCounter integer not null );" ;
public DBAdapter(Context ctxt)
{
this.context=ctxt;
DBHelper= new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(EXERCISE_TABLE_CREATE);
db.execSQL(TRACKER_TABLE_CREATE);
db.execSQL(Meal_TABLE_CREATE);
db.execSQL(FOOD_TABLE_CREATE);
db.execSQL(MealFOOD_TABLE_CREATE);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// Log.w(TAG, "Upgrading database from version" + oldVersion +" to "+ newVersion + ", which will destroy all old data");
// db.execSQL("DROP TABLE IF EXISTS Exercise");
// db.execSQL("DROP TABLE IF EXISTS Food");
// db.execSQL("DROP TABLE IF EXISTS Meal");
// db.execSQL("DROP TABLE IF EXISTS MealFood");
//
// onCreate(db);
}
}
//--open the DB
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert Exercise info to the Exercise table---
public long SaveExecise(String date ,String time,String name ,float period, float BurnedCalories)
{
ContentValues content = new ContentValues();
content.put(KEY_DATE, date);
content.put(KEY_TIME, time);
content.put(KEY_NAME, name);
content.put(KEY_PERIOD,period);
content.put(KEY_BURNEDCALS, BurnedCalories);
return db.insert(EXERCISE_TABLE_NAME, null, content);
}
// retrieve ex_Name ,ex_period, ex_burned cals of all exercises played in a specific date
public Cursor getExerciseInfo(String date) throws SQLException
{
Cursor C_excer = db.query(EXERCISE_TABLE_NAME, new String[] {KEY_NAME,KEY_PERIOD,KEY_BURNEDCALS},
KEY_DATE + " = '" + date + "'", null, null, null, null);
//if (C_excer != null) {
C_excer.moveToFirst();
//}
return C_excer;
}
// insert meal info to the meal table
public long SaveMeal(String date , String time , String mealType)
{
ContentValues content = new ContentValues();
content.put(KEY_MDATE,date);
content.put(KEY_MTIME,time);
content.put(KEY_MEALTYPE,mealType);
return db.insert(MEAL_TABLE_NAME, null, content);
}
// insert Food info to the Food table
public long SaveFood(String name,int calories,int Vit_B12,int cholesterol,int protein ,int iron ,int sodium,int Fat_Mono,int Fat_Sat,int carbohydrate)
{
ContentValues content = new ContentValues();
content.put(KEY_FOODNAME,name);
content.put(KEY_CALORIES,calories);
content.put(KEY_VB12,Vit_B12);
content.put(KEY_CHOLESTEROL,cholesterol);
content.put(KEY_PROTEIN,protein);
content.put(KEY_IRON,iron);
content.put(KEY_SODIUM,sodium);
content.put(KEY_FAT_MONO,Fat_Mono);
content.put(KEY_FAT_Sat,Fat_Sat);
content.put(KEY_CARBOHYDRATE,carbohydrate);
return db.insert(FOOD_TABLE_NAME, null, content);
}
// get food id by its name
public int getFoodIDByName(String name) throws SQLException
{ int id =0;
Cursor cursor=db.query(true,FOOD_TABLE_NAME, new String[]{KEY_FOODID}, KEY_FOODNAME+ " = '" + name + "'", null, null, null, null,null);
if (cursor != null) {
cursor.moveToLast();
id=cursor.getInt(cursor.getColumnIndex(KEY_FOODID));
}
cursor.close();
cursor.deactivate();
return id;
}
// insert mealFood info to mealFood table
public long SaveMealFood(String date , String time , String mealType, int Food_id)
{
ContentValues content = new ContentValues();
content.put(KEY_MFDATE,date);
content.put(KEY_MFTIME,time);
content.put(KEY_MFMEALTYPE,mealType);
content.put(KEY_MFFOODID,Food_id);
return db.insert(MEALFOOD_TABLE_NAME, null, content);
}
// get specific meal foodID from mealFood table
public Cursor getSpecificMealFoodsID(String date,String mealType) throws SQLException
{
Cursor cursor=db.query(true,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;
}
//get food name,cals from food table based on the food id that we get it from the mealFood table
public Cursor getFoodNameAndCals(int food_id) throws SQLException
{
Cursor cursor=db.query(true,FOOD_TABLE_NAME, new String[]{KEY_FOODNAME,KEY_CALORIES},KEY_FOODID+"="+food_id, null, null, null, null,null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
public Cursor getAllFoods() throws SQLException
{
Cursor food_Cursor = db.query(FOOD_TABLE_NAME, new String[] {KEY_FOODID, KEY_FOODNAME,KEY_CALORIES}, null, null, null, null, null);
if (food_Cursor != null) {
food_Cursor .moveToFirst();
}
return food_Cursor ;
}
// retrieve all store exercises // for testing
public Cursor getExerciseInfo() throws SQLException
{
Cursor C_excer = db.query(EXERCISE_TABLE_NAME, new String[]
{
KEY_NAME,KEY_PERIOD,KEY_BURNEDCALS},null, null, null, null, null);
//if (C_excer != null) {
C_excer.moveToFirst();
//}
return C_excer;}
}
In my application activities i just used the methods for inserting and retrieving data
what i should do to prevent the recreation of the data base in each time i ran the app ?
since i think its true what you are saying , please help me
I see nothing wrong with your code. Has your code in the
onUpgrademethod always been commented out?My guess would be you incremented the database version, which triggered the
onUpgrademethod and deleted your tables then re-created them (assuming that that code hasn’t always been commented out).