4

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.

0

4 Answers 4

8

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);
Sign up to request clarification or add additional context in comments.

5 Comments

Hi , instead of passing null I want to pass an String array that contains '5' and 'comic'. How to do this
you don't need to pass them explicitly 2nd time. see the query properly.i have already put that condition in 3rd argument.
can I use like this int id=sqdb.update("table_name",values,"bookid=? and booktype=?",array); where array contains 5 and comic
Why is your update() call returning an id #? The docs say it returns the number of records updated. (Mine always returns 0, even when successful.)
@Carol: yes...my mistake it was.it would return number of rows updated.But i have no clue why you are getting 0 even when update is successful.
4

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);

Comments

2

use this code

public void updatefriendpic(String fuid, String temp, String temp1) {

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(THUMB_FRIEND, temp);
        values.put(CONTENT_FRIEND, temp1);
        // update Row
        db.update(TABLE_FRIEND_LIST,values,"_id = '"+fuid+"'",null);
        db.close(); // Closing database connection
        Log.d(TAG, "New user update into sqlite: ");

    }

Comments

0
public boolean updateStopnameOfRute(String route,String stopname,int sequence)
        {
            DATABASE_TABLE = "tbl_Stop";
            ContentValues contentvalue=new ContentValues();
            contentvalue.put("stop_name", stopname);
            return db.update(DATABASE_TABLE, contentvalue, "route_name='"+route+"'" +"and"+ "stop_sequence="+sequence, null)>0;
        }

Comments

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.