How to use SQLite's "whereClause" inside update method - android

I'm dealing with an sqlite db in android application.
To update a row in the db I use method of SQLiteDatabase object "update"
SQLiteDatabase.update(String table, ContentValues values, String whereClause, String[] whereArgs)
But here a confusion, how "whereClause" should look like?
Assume I have these values for update.
values.put("name", appInfo.getName());
values.put("package_name", appInfo.getPackageName());
values.put("version_name", appInfo.getVersionName());
I want to update a row where package_mane column equal to "com.mynamespace.db". How "whereClause" should be written here? Thanks.

db.update("table", values, "package_name = ?", new String[]{"com.mynamespase.db"});
Another example:
db.update("table", values, "package_name = ? and other_field = ?", new String[]{"com.mynamespase.db", "test"});

You whereClause should look like this:
String whereClause = "package_mane = ?"; // package_name perhaps?
String[] whereArgs = new String[]{"com.mynamespase.db"};
String from whereArgs are used to substitute ? in whereClause.

You should use it like this:
SQLiteDatabase.update(table, "package_name=?", new String[] { "com.mynamespace.db" });
and prevent other problems and cache inefficiencies if you concat the string.

db.update(TABLE_NAME, values, "package_name=com.mynamespase.db", null);
// or
String[] args = { "com.mynamespase.db" };
db.update(TABLE_NAME, values, "package_name=?", args);

Related

Android : database not updated

I want to update a field in my database, but the changes not saved to database.
I use The "update" method in database class but not work
Please
This is my Method
public void myupdatedb(int id,String value){
ContentValues cv = new ContentValues();
cv.put("fav", "1");
mydb.update("contents", cv, "id" + "=" + id , null);
}
and in the MainActivity:
db.myupdatedb(1,"1");
From Android documentation
public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.
Try
mydb.update("contents", cv, "id = ?", new String[]{Integer.toString(id)});

Edit SQLite database record

How can I edit a particular record in a SQLite table given, Database Name, Table Name, int _id ,Column Name and Desired value of record?
EDIT:
My solution was database.update(DataBaseHelper.VFS_DATABASE_TABLE, values, "_id=?", new String[] {id+""});
public int deleteCpShadowEntryById (int id) {
int delRows = mDb.delete(<your_table_name>, "_id" + "=?", new String[] {String.valueOf(id)});
return delRows;
}
But I guess you do not want to delete. You want to update your value. So in this case you should at first select your row, fill the values of this row into a new ContentValues, replace you old value with new and update the row.
Edit: Maybe try this:
ContentValues args = new ContentValues();
args.put(<your_columnName>, newValue);
db.update(<your_table_name>, args, "_id" + "=?", new String[] {String.valueOf(id)});
Should be a simple bit of SQL:
DELETE FROM <table name> WHERE <column name> = <desired value>
SQLiteDatabase has a delete method :
public int delete (String table, String whereClause, String[] whereArgs)
If you have id of the particular record the code would be something like :
String whereArgs[] = new String { id };
db.delete(TABLE_NAME, "_id = ?", whereArgs);

Update method doesn't update database with value

Here is my piece of code which runs without errors, but doesn't update database:
ContentValues cv_lio = new ContentValues();
cv_lio.put("u_lio",1);
String where = "u_name" + "=" + 5;
db.update(table_name, cv_lio,where,null);
Can anyone improve my update query to work well.
Proceed as follow :
ContentValues cv_lio = new ContentValues();
cv_lio.put("u_lio",1);
String where = "u_name" + " = ?";
String[] whereArgs = new String[] {String.valueOf(5)};
db.update(table_name, cv_lio, where, whereArgs);
please refer to documentation which is, I must confess, not so clear on that precise point...
The error was at where clause corrected code can be
ContentValues cv_lio = new ContentValues();
cv_lio.put("u_lio",1);
String where = "u_name='" + un + "'";
db.update(table_name, cv_lio,where,null);
Look for Quotes when you want to show field value like the one shown above..

String[] whereArgs parameter of database method in android

delete(String table, String whereClause, String[] whereArgs)
update(String table, ContentValues values, String whereClause, String[] whereArgs)
What is this String[] whereArgs? Is it connected with "?" (so called wild card)?
Yes, the String[] whereArgs contains the arguments to be appended to the whereClause.
For example, you want to make a delete query:
delete from InfoTable where name = "ABC" and id = "23"
then the query should be:
delete("InfoTable", "name = ? AND id = ?" , new String[] {"ABC", "23"});

Cannot update multiple rows in Sqlite database update command in android

I am facing a problem here. I am trying to bulk update my table i.e trying to update multiple rows in the database. The update is simple. I just need to set a column value to 1 which is actually used as a flag in the app. So I want to set the value to 1 and in the where clause I give the string array of all the ids where i want it to set.
But the problem is that it gives me an "android.database.sqlite.SQLiteException: bind or column index out of range:" . However when a single value is provided either as string or an string array, the update works fine.
here is the query
db.update( tableName, cv, Z_ID + "=?", items );
where items is of type String[ ]
kindly tell me what am I missing?
regards
Fahad Ali Shaikh
That is not going to work I believe based on the one line you gave.
If we look at the method call signature :
update(String table, ContentValues values, String whereClause, String[] whereArgs)
so:
ContentValues cv=new ContentValues(); cv.put(colName, new Integer(1));
String[] items = new String []{String.valueOf(Z_ID)}
db.update( tableName, cv, Z_ID + "=?", items );
Does you code look something like this?
Your items array must match up with the where clause. A different example would look something like this:
ContentValues cv=new ContentValues(); cv.put(salesColName, new Integer(4534));
String whereClause = "band=? and album=?"; String whereArgs = new String [] { "U2", "Joshua Tree" };
db.update( tableName, cv, whereClause , whereArgs);

Categories

Resources