Developer Developer - 25 days ago 9
Java Question

SQLite DB Deleted After Restarting App

For some reason when I restart my Android app, the database I created previously is gone. My helper class is shown below. I create an instance of the helper class in a separate fragment and only call the following functions from the fragment:

helper = new LocationBaseHelper(getActivity());
helper.insertEntry(blah blah);


If it matters, I create the database in the "onCreateView( )" fragment class. I test the size of the database each time I add, so I know that it is indeed adding to the database. When I restart and check the size, it is 0 again. Here is my helper class.

// Edit

If someone could explain how SQLite DBs behave on Android, that would also help. If I call the helper constructor multiple times, do multiple DBs get created? Do they overwrite previous ones? What if I call it from different fragments/activities/services? Thank you.

public class LocationBaseHelper extends SQLiteOpenHelper {

private static final int VERSION = 1;
private static final String D​A​T​A​B​A​S​E​_​N​A​M​E​ = "locationBase.db";
private int mNumberOfElements = 0;

public LocationBaseHelper(Context context) {
super(context, D​A​T​A​B​A​S​E​_​N​A​M​E​, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + LocationTable.NAME + " (" +
LocationTable.Cols.DATE_TIME + " text, " +
LocationTable.Cols.LATITUDE + " text, " +
LocationTable.Cols.LONGITUDE + " text )"
);
}

// do nothing
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}

public void insertEntry(String date_time, String latitude, String longitude){
mNumberOfElements++;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues content = new ContentValues();
content.put(LocationTable.Cols.DATE_TIME,date_time);
content.put(LocationTable.Cols.LATITUDE,latitude);
content.put(LocationTable.Cols.LONGITUDE,longitude);
db.insert(LocationTable.NAME,null,content);
}

public ArrayList<String> getEntireDatabase(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + LocationTable.NAME,null);
cursor.moveToFirst();

ArrayList<String> values = new ArrayList<>();

do{
String value = (String) cursor.getString(cursor.getColumnIndex(LocationTable.Cols.DATE_TIME)) + " " +
(String) cursor.getString(cursor.getColumnIndex(LocationTable.Cols.LATITUDE)) + " " +
(String) cursor.getString(cursor.getColumnIndex(LocationTable.Cols.LONGITUDE));
values.add(0,value);

}while(cursor.moveToNext());

return values;
}

public int size(){return mNumberOfElements;}
}

Answer

I test the size of the database each time I add, so I know that it is indeed adding to the database. When I restart and check the size, it is 0 again

If by "check the size" you mean this stuff:

private int mNumberOfElements = 0;

public void insertEntry(String date_time, String latitude, String longitude){
    mNumberOfElements++;

...

public int size(){return mNumberOfElements;}
}

then of course an another instance of your helper starts counting inserts from 0 again as fields are initialized to their default values.

To count the number of elements in a database table, do a COUNT(*) query. For example:

SQLiteDatabase db = getReadableDatabase();
long numberOfRows = DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM " + LocationTable.NAME, null);
Comments