10

The table contains 4 columns : rowID , word , defintition , group_id

I want to change a certain row's word and definition . So here is my code (word is an object where the word , definition , id and the group_id are stored)

   ContentValues values = new ContentValues();
    values.put(KEY_WORD, word.getWord());
    values.put(KEY_DEFINITION, word.getDefinition());
    db.update(TABLE_WORDS, values, KEY_ID, new String [] {String.valueOf(word.getID())});

Can anyone tell me why it only creates a new line instead of changing the row under given ID ?

6 Answers 6

10
SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName());
        values.put(KEY_DATE, contact.getDate());

        // updating row
        return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(contact.getID()) });
Sign up to request clarification or add additional context in comments.

Comments

4
   String id = "1";
   SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COLUMN_NOTIFICATION_STATUS,"canceled");
    db.update(TABLE_NOTIFICATION, values,COLUMN_NOTIFICATION_ID + " = ? ",new String[]{ String.valueOf(id) });

it will work as prepared statement. this piece of code worked in my case .. Thanks

Comments

1

@Digvesh has the right idea, but because of this limitation, converting an int to a String to use as a selection arg will not work properly. Instead do this:

// assume: SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_WORD, word.getWord());
values.put(KEY_DEFINITION, word.getDefinition());
int rowsUpdated = db.update(TABLE_WORDS, values, KEY_ID + "=" + word.getID(), null);

Comments

1

this work for me, while db.rawquery didnot work.

output query

String qu="UPDATE "+ DATABASE_TABLE_DATA + " SET "+isbookmark+" = "+status+" WHERE id = "+uid+";";
db.execSQL(qu);

output query :
UPDATE tabel_data SET `isbookmark` = 1 WHERE id = 2720271;

Comments

0

Use LIKE instead of = operator.

SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName());
        values.put(KEY_DATE, contact.getDate());

        // updating row
        return db.update(TABLE_CONTACTS, values, KEY_ID + " LIKE ?",
                new String[] { String.valueOf(contact.getID()) });

Comments

-2

You can try this using db.rawQuery , i.e.

db.rawQuery("UPDATE "+ TABLE_WORDS + " SET "+ KEY_WORD + " = '"+word.getWord()+"' ,"+KEY_DEFINITION+"= '"+word.getDefinition()+ "' WHERE " + KEY_ID + " = "+word.getID(), null);

Here you don't need to create any ContentValues.

here and here are details

6 Comments

I tried the same query with db.exec (). So what is the difference between those two methods ?
@user3199577 execSQL(String sql) and execSQL(String sql, Object[] bindArgs) both method do not return any data, but rawQuery(String sql, String[] selectionArgs) returns data into Cursor object
@user3199577 Let me know if there any problem in above query.
This is the error The method rawQuery(String, String[]) in the type SQLiteDatabase is not applicable for the arguments (String)
I don't see a reason to have to run the raw query and overcomplicate things unnecessarily. update() will work if you know how you're storing data in your DB and you structure your update() query properly. Please see my answer as this should work.
|

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.