Android的SQLite的数据丢失 [英] Android SQLite data loss
问题描述
我一直在使用的SQLite
在我的应用程序存储一些数据在应用程序中使用。
我面临的问题是,如果让说,我填的表今天一些数据,并显示在应用程序,数据依旧很好,不会丢失。
I've been using SQLite
in my app to store some data to be used in the app.
The problem I'm facing is that if lets say I fill the tables today with some data and show them in the app, the data persists fine and doesn't get lost.
然而,当我来到了第二天的尝试以显示我的前一天输入的数据,该数据似乎已丢失的(或者整个数据库/表
重新创建,我也不太清楚)
However when I come in the next day and try to display the data I entered in the day before, the data seems to have been lost (Or maybe the whole database/tables
are recreated, I'm not too sure)
我写的数据库
的方法是如下:
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;
}
}
任何人有为什么数据似乎是在第二天消失任何想法?
Anyone has any idea why the data seems to disappear in the following day?
推荐答案
打开注释成一个答案; - )
Turning the comment into an answer ;-)
好吧,你居然放弃你使用这个类数据库每次,因为你已经实现了 onUpgrade
方法。
Ok, you're actually dropping the database everytime you're using this class, because you've implemented the onUpgrade
method.
这方法只有当你改变你的数据库,因此需要更新表定义的。删除 onUpgrade
方法内,你应该罚款;-)
This method is only for when you're changing your database and hence need to upgrade the table definitions. Remove the inside of the onUpgrade
method and you should be fine ;-)
同样在这里的 SQLiteOpenHelper
类阅读起来:<一href=\"http://developer.android.com/reference/android/database/sqlite/%E2%80%A6,%20int,%20int%29\">developer.android.com/reference/android/database/sqlite/…, INT,INT)
Also read up on the SQLiteOpenHelper
class here: developer.android.com/reference/android/database/sqlite/…, int, int)
这篇关于Android的SQLite的数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!