2012-05-31 65 views
-1

在我的應用程序中,我使用sqlit數據庫存儲用戶信息,並且這些信息存儲成功,但它們存儲了2天,因爲我可以在2天內檢索它們並在屏幕上顯示它們,但是在2天或更長時間後我運行我的應用程序沒有顯示數據「當我調試我的代碼時返回的遊標是空的」我使用DataBaseAdapter和DatabaseHelper類來創建數據庫,我真的很想知道爲什麼會出現此問題?我怎麼能解決它?
我認爲,在代碼中沒有問題,因爲我可以插入和檢索數據,我發現在數據/數據/我的pavkage名稱/數據庫我的數據庫/數據庫名稱數據存儲在我的sqlite數據庫中只有幾天?

任何機構對這個問題的想法?請幫幫我......

這是我的代碼來創建數據庫

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;} 
} 

在我的應用程序的活動,我只是用於插入和檢索數據 我應該做的方法每次運行應用程序時都阻止重新創建數據庫? 因爲我認爲它是真的你在說什麼,請幫我

+1

SQLite不會每兩天刪除它的內容。你必須檢查你的代碼,看看你在做什麼。也許你正在刪除舊數據?當你認爲你正在初始化時重新創建數據庫? –

+0

郵政編碼,如果你需要幫助,我們不是介意的讀者 – Barak

+0

正如@Panagiotis Kanavos建議的那樣,我的猜測也是你正在重新創建數據庫。 – theAlse

回答

0

我沒有看到你的代碼錯了。您的代碼在onUpgrade方法中是否始終被註釋掉?

我的猜測是你增加了數據庫版本,它觸發了onUpgrade方法並刪除了你的表,然後重新創建了它們(假設代碼並未總是被註釋掉)。

+0

onUpgrade方法代碼總是作爲註釋 – user

+0

是否有另一個可能導致數據庫重新創建的事情? – user

+0

請幫我,我該怎麼辦? – user

相關問題