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 Answers 2

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:

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.