在我的应用程序中,我使用 sqlit 数据库来存储用户信息,并且这些信息存储成功,但是它们存储了 2 天,因为我可以在 2 天内检索它们并将它们显示在屏幕上,但是在我运行 2 天或更长时间之后我的应用程序没有显示数据“调试代码时返回的光标为空”我使用 DataBaseAdapter 和 DatabaseHelper 类来创建数据库,我真的很想知道,为什么会出现这个问题?我该如何解决?
我相信代码没有问题,因为我可以插入和检索数据,并且我在 data/data/my pavkage name /databases/ DBname 中找到了我的数据库
有没有人知道这个问题?请帮我 ......
这是我创建数据库的代码
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;}
}
在我的应用程序活动中,我只是使用了插入和检索数据的方法,我应该怎么做才能防止在每次运行应用程序时重新创建数据库?因为我认为你说的是真的,请帮助我