1

I have a Problem with my sqlite database. Whenever i insert something in my db, it is inserted right, i can tell because i can find it in the debug mode with all its entries. However when i close the db with db.endTransaction(); the data seems lost because whenever i reopen it to read from the db it cant find any entries...even so the app starts without exceptions.

Can anyone help me?

thanks, thoorbenVerdorben

    public boolean storeValues(Context context, List<Course> values){
    db = DBOpenHelper.getInstance(context).getWritableDatabase();
    long id = 0;

    db.beginTransaction();


    for(values value: myvalues){
        ContentValues cv = new ContentValues();
        cv.put(DatabaseContract.HomeTable.name,value.name);
        cv.put(DatabaseContract.HomeTable.link,value.name);
        cv.put(DatabaseContract.HomeTable.id,value.name);
        id = db.insert(DatabaseContract.HomeTable.TABLE_NAME,null,cv);
    }
    db.endTransaction();
    if(id<0 ){
        return false;

    }
    else return true;
}


    public Set<values> getValues(Context context){
    db = DBOpenHelper.getInstance(context).getReadableDatabase();
    db.beginTransaction();
    String[] columns = {DatabaseContract.HomeTable.name,DatabaseContract.HomeTable.link,DatabaseContract.HomeTable.id};
    Cursor cursor = db.query(DatabaseContract.HomeTable.TABLE_NAME, columns,null,null,null,null,null);
    Set<Course> mySet = new LinkedHashSet<Course>();

    while(cursor.moveToNext()) {
        Values value = new Values();

        value.setName(cursor.getString(0));
        value.setName(cursor.getString(1));
        value.setName(cursor.getString(2));

        mySet.add(value);
    }

    db.endTransaction();

    return mySet;
}

2 Answers2

2

You're never closing the connection or committing the changes.

setTransactionSuccessful()

I think is what you're looking for. Check here for more info:

Community
  • 1
  • 1
amza
  • 780
  • 2
  • 7
  • 32
1

I would change your while() loop to:

if(cursor.moveToFirst()) { // Ensure the cursor starts from the beginning
    do {
        Values value = new Values();

        value.setName(cursor.getString(0)); // Hopefully you realize you're 
        value.setName(cursor.getString(1)); // setting all these to the
        value.setName(cursor.getString(2)); // same property

        mySet.add(value);
    } while(cursor.moveToNext());
}
if(cursor != null) { cursor.close(); }

If that doesn't work, you would need to probably provide your DbHelper code

jyanks
  • 2,356
  • 1
  • 19
  • 36
  • I didn't even see that you hadn't set the transaction to successful - that is the right answer. That being said, you should still be doing the moveToFirst() check and the cursor.close() as a best-practice – jyanks May 22 '15 at 18:13