2012-05-31 132 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

相关问题