Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using update statement in sqlite in android

Tags:

android

sqlite

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.

like image 479
Prabhu M Avatar asked Dec 10 '22 04:12

Prabhu M


2 Answers

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);
like image 153
Hiral Vadodaria Avatar answered Dec 21 '22 11:12

Hiral Vadodaria


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);
like image 36
Jebasuthan Avatar answered Dec 21 '22 11:12

Jebasuthan