0

I have a problem where my sqlite table "views" doesn't update upon a update query.

The views table is constructed as follows, my two foreign keys making up for the identifying primary key:

CREATE TABLE views
(description TEXT, 
modulesId INTEGER NOT NULL REFERENCES modules(modulesId),
eventId INTEGER NOT NULL REFERENCES events(eventId),
PRIMARY KEY (modulesId, eventId));

Using the following code I attempt, and it would appear, succeed in updating the table, as the returned integer takes the result of 1.

private SQLiteDatabase database;    

public void updateViews(String description, int modulesId, int eventId) {
    ContentValues values = new ContentValues();
    values.put(MyDatabaseManager.VIEWS_DESCRIPTION, description);
    String where = "modulesId = " + modulesId + " AND eventId = " + eventId;

    int i = database.update(MyDatabaseManager.TABLE_VIEWS, values, where, null);
    }

So. int i is returned a 1, indicating that my row is indeed updated. However as I attempt to retrieve the rows I find that the description of the supposedly updated row is still null.

public List<Views> getAllViews() {
    List<Views> views = new ArrayList<Views>();
    Cursor cursor = database.query(MyDatabaseManager.TABLE_VIEWS, allViewsColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Views view = cursorToView(cursor);
        views.add(view);
        cursor.moveToNext();
    }
    cursor.close();
    return views;
}

This is true without closing the application.

I have no visible errors in my code, and nor do I seem to be able to make out much of my logcat. Which leads me to believe I have done something fundamentally wrong somewhere in my code. Most likely in code that I haven't posted, but perhaps you can make out where I might have gone wrong.

I am not very proficient at programming but I will do my best to follow any help that I get. Thanks!

Update 1:

That looks promising, inmyth. I will give that a good looking over. Thanks!

private Views cursorToView(Cursor cursor) {
    Views view = new Views();
    view.setModuleId(cursor.getLong(0));
    view.setEventId(cursor.getLong(1));
    return view;
}

Update 2: Answer

Additionally I had forgotten to update my allViewsColumn with my description column, but that is not a part of this specific question, but could possibly help someone out.

private Views cursorToView(Cursor cursor) {
    Views view = new Views();
    view.setModuleId(cursor.getLong(0));
    view.setEventId(cursor.getLong(1));
    view.setViewDescription(cursor.getString(2));
    return view;
}
Dennis
  • 169
  • 9

1 Answers1

1

There you go. You didn't load the description in cursorToView().

Next time you can read your own database to see if any entry is missing provided you run an emulator. How to view data saved in android database(SQLite)?

Community
  • 1
  • 1
inmyth
  • 8,880
  • 4
  • 47
  • 52