Using update statement in sqlite in android

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 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 on the Internet, but I am not getting related examples. please, help.

+4
source share
5 answers

You can update it with 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 the code below where you need to update the line:

 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); 
+9
source

Try it. It can help 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); 
+4
source

this link is useful for this type of request (String whereClause, String [] whereArgs)

+3
source

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: "); } 
+2
source
 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; } 
0
source

Source: https://habr.com/ru/post/1380322/


All Articles