John Snrub
John Snrub

Reputation: 11

updating sqlite database using a String not working

This works but i need to be able to change the value of the product_name

String query = "UPDATE UserTable SET _quantity=_quantity - 1 WHERE product_name ='Hammer'";
myDatabase.execSQL(query);

This does not work and I can't see why.

String test = "Hammer";
String query = "UPDATE UserTable SET _quantity=_quantity - 1 WHERE product_name ='%"+test+ "%'";
myDatabase.execSQL(query);

Upvotes: 0

Views: 59

Answers (3)

GrIsHu
GrIsHu

Reputation: 23638

You need to remove % symbols from your query. This symbols are used to match the strings values if you want to match anything like what he wants %mer% would match hammer

Try as below:

String query = "UPDATE UserTable SET _quantity=_quantity - 1 WHERE product_name ='"+test+ "'";

Upvotes: 0

android_dev
android_dev

Reputation: 1477

Try this code

i think it will work

ContentValues data = new ContentValues();
data.put("Column Name", column_value);

i.e,

data.put("_quantity", 3);

myDatabase.update(TABLE_NAME ,data,"product_name = ?",new String[]{test});

Upvotes: 1

Bismark Ito
Bismark Ito

Reputation: 651

To match a pattern you should use the LIKE statement:

String test = "Hammer";
String query = "UPDATE UserTable SET _quantity=_quantity - 1 WHERE product_name LIKE '%"+test+ "%'";
myDatabase.execSQL(query);

Upvotes: 0

Related Questions