1

我一直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;
    }

}

任何人都知道为什么数据似乎在第二天消失了?

4

1 回答 1

6

将评论变成答案;-)

好的,您实际上每次使用此类时都会删除数据库,因为您已经实现了该onUpgrade方法。

此方法仅适用于您更改数据库并因此需要升级表定义时。删除onUpgrade方法的内部,你应该没问题;-)

还在这里阅读SQLiteOpenHelper课程:developer.android.com/reference/android/database/sqlite/…, int, int)

于 2013-02-28T22:13:36.590 回答