0

I'm currently developing an android app with eclipse. I got a problem while updating the database. The problem is the app crash while the updateFlag method is called. The updateFlag method only update one specific column only based on the rowId.

This is my database structure in SQLiteAdapter class:

public long insert(String answer, String question, String hint, String flag, String level){

  ContentValues contentValues = new ContentValues();
  contentValues.put(KEY_ANSWER, answer);
  contentValues.put(KEY_QUESTION, question);
  contentValues.put(KEY_HINT, hint);
  contentValues.put(KEY_FLAG, flag);
  contentValues.put(KEY_LEVEL, level);
  return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
 }

This is the update method in SQLiteAdapter class:

public void updateFlag(long rowId)
    {
        ContentValues args = new ContentValues();
        args.put(KEY_FLAG, "1");
        sqLiteDatabase.update(MYDATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null);
    }

And this is how I call the updateFlag method in another class:

        mySQLiteAdapterListener2 = new SQLiteAdapter(this);
        mySQLiteAdapterListener2.openToWrite();
        long idListener = Long.parseLong(getId);
        mySQLiteAdapterListener2.updateFlag(idListener);
        mySQLiteAdapterListener2.close();

What's wrong with my code? Anyone know how to update one specific column based on rowId in the right way?

Bobby Chandra
  • 89
  • 2
  • 10

2 Answers2

2

SQLiteDatabase update method takes four arguments:

database.update(String table_name, ContentValues values, String selection, String[] selectionArgs);

So, your query should be like in your below updateFlag method. Replace your updateFlag method with this one and try this.

public void updateFlag(long rowId)
    {
        ContentValues args = new ContentValues();
        args.put(KEY_FLAG, "1");
        sqLiteDatabase.update(MYDATABASE_TABLE, args, KEY_ROWID + "=?", new String[]{rowId+""});
    }
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
  • The documentation actually states that you can give null as the two last arguments.http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#update(java.lang.String, android.content.ContentValues, java.lang.String, java.lang.String[])) – DigCamara May 15 '13 at 17:48
  • @DigCamara Yes ofcourse. In that case all the rows will be affected. Check the documentation once again. In the present scenario, if you pass `NULL`, `NULL` in the last two arguments, then all the rows will be updated to the value "1" in the column "KEY_FLAG". – Chintan Soni May 16 '13 at 04:02
1

Try this:

sqLiteDatabase.update(MYDATABASE_TABLE, args, KEY_ROWID + "=?", new String[] {rowId});

The last parameters if for passing the values that will replace the '?'.

This assumes you have a problem with your update. You need to post a stacktrace so we can see where the app is crashing.

Nick
  • 1,340
  • 1
  • 15
  • 23