I want to update a row in sqlite. My query looks like this
update name="aaa",publisher="ppp",price="111" where bookid=5 and booktype="comic"
I want to update by using the following update command .
int android.database.sqlite.SQLiteDatabase.update(String table, ContentValues values, String whereClause, String[] whereArgs)
What should be the parameters for "whereClause" and "whereArgs". I am searching in Internet but I am not getting related examples. please help me.
You can update it using this code:
DatabaseCreator.class:
public class DatabaseCreator extends SQLiteOpenHelper{
private static final String DB_NAME="database_name";
private static final int DB_VER=1;
private static final String TABLE_NAME="<table creation query>";
public DatabaseCreator(Context context) {
super(context,DB_NAME, null, DB_VER);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(TABLE_NAME);
}
@Override
public void onUpgrade(SQLiteDatabase database, int arg1, int arg2) {
database.execSQL("DROP TABLE IF EXISTS table_name");
onCreate(database);
}
}
Now use below code where you need to update the row:
DatabaseCreator dbcreator=new DatabaseCreator(context);
SQLiteDatabase sqdb=dbcreator.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name","aaa");
values.put("publisher","ppp");
values.put("price","111");
int id=sqdb.update("table_name",values,"bookid='5' and booktype='comic'",null);
Try this. It could helpful for you
db.execSQL("UPDATE DB_TABLE SET YOUR_COLUMN='newValue' WHERE id=6 ");
or
ContentValues newValues = new ContentValues();
newValues.put("YOUR_COLUMN", "newValue");
or
ContentValues newValues = new ContentValues();
newValues.put("YOUR_COLUMN", "newValue");
String[] args = new String[]{"user1", "user2"};
db.update("YOUR_TABLE", newValues, "name=? OR name=?", args);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With