我一直在我的应用中使用SQLite
来存储一些要在应用中使用的数据。
我面临的问题是,如果我今天用一些数据填充表格并在应用程序中显示它们,数据仍然存在并且不会丢失。
然而,当我第二天来到尝试显示我前一天输入的数据时,数据似乎已丢失(或者可能重新创建整个database/tables
,我不太确定)
我写给database
的方式如下:
public class CustomExercisesDB
{
private static final String DB_NAME = "app";
private static final int DB_VERSION = 15;
private static final String ChosenExercises = "ChosenCustExs";
public static final String ChEX_ID = "ChExId";
public static final String Cust_NAME = "CustomName";
public static final String ChEx_NAME = "ChExName";
public static final String ChEx_Weight = "ChExWeight";
public static final String ChEx_Reps = "ChExReps";
public static final String ChEx_Sets = "ChExSets";
private DBHelper helper;
private final Context context;
private SQLiteDatabase database;
private static class DBHelper extends SQLiteOpenHelper
{
public DBHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
}
public void onCreate(SQLiteDatabase db)
{
String CREATE_TABLE_SITEINFO = "CREATE TABLE ChosenCustExs(ChExId INTEGER PRIMARY KEY AUTOINCREMENT, CustomName TEXT NOT NULL, ChExName TEXT NOT NULL, ChExWeight INTEGER, ChExReps INTEGER, ChExSets INTEGER);";
db.execSQL(CREATE_TABLE_SITEINFO);
}
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2)
{
db.execSQL("DROP TABLE IF EXISTS " + ChosenExercises);
onCreate(db);
}
}
public CustomExercisesDB(Context c)
{
context = c;
}
public void close()
{
helper.close();
}
/**opens the database and allows us to write to it**/
public CustomExercisesDB open()
{
helper = new DBHelper(context);
/**this would allow us to write to the database and insert records**/
database = helper.getWritableDatabase();
return this;
}
public void populateChosenExs(String custName,String chName,String chWeight,String chReps,String chSets)
{
database.beginTransaction();
try
{
ContentValues cv = new ContentValues();
cv.put(Cust_NAME, custName);
cv.put(ChEx_NAME , chName);
cv.put(ChEx_Weight, chWeight);
cv.put(ChEx_Reps, chReps);
cv.put(ChEx_Sets, chSets);
database.insert(ChosenExercises, null, cv);
database.setTransactionSuccessful();
}
finally
{
database.endTransaction();
}
}
public String getChosenExs()
{
String[] columns = new String[]{ChEX_ID, Cust_NAME,ChEx_NAME,ChEx_Weight,ChEx_Reps,ChEx_Sets};
Cursor c = database.query(ChosenExercises, columns, null, null, null, null, null);
String result = "";
int id = c.getColumnIndex(ChEX_ID);
int custName = c.getColumnIndex(Cust_NAME);
int chEx = c.getColumnIndex(ChEx_NAME);
int chW = c.getColumnIndex(ChEx_Weight);
int chR = c.getColumnIndex(ChEx_Reps);
int chS = c.getColumnIndex(ChEx_Sets);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
{
/**gets the value of each column from each row and stores it in the result variable**/
result = result + c.getString(id)+" "+c.getString(custName)+" "+c.getString(chEx)+" "+c.getString(chW)+" "+c.getString(chR)+" "+c.getString(chS)+"\n";
}
return result;
}
}
任何人都知道为什么数据似乎在第二天消失了?
答案 0 :(得分:6)
将评论转为答案; - )
好的,每次使用此类时,您实际上都在删除数据库,因为您已经实现了onUpgrade
方法。
此方法仅适用于更改数据库并因此需要升级表定义的情况。删除onUpgrade
方法的内部,你应该没事;-)
另请阅读SQLiteOpenHelper
课程:developer.android.com/reference/android/database/sqlite/…, int, int)