简体   繁体   中英

Android SQLite data loss

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)

The way I'm writing to the database is as following:

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 ;-)

Ok, you're actually dropping the database everytime you're using this class, because you've implemented the onUpgrade method.

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 ;-)

Also read up on the SQLiteOpenHelper class here: developer.android.com/reference/android/database/sqlite/…, int, int)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM