Cannot update multiple rows in Sqlite database update command in android - 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);

Related

Update value to specific primary key in sqlite , android?

I have to below table structure of my sqlite database in android
I want to update value to "answer" column according to the ques id.
How do I do it?
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_Answer, answer);
// Inserting Row
db.insert(TABLE_Question, null, values);
db.close(); // Closing database connection
I want to insert value to "answer" column according to the ques id.
This operation called "update" instead of "insert".
insert: means to add new record in table,
update: means change value of any exist row columns
so required operation is update.use update method as:
String where = "Ques_id=?";
String[] whereArgs = new String[] {String.valueOf(Ques_id)};
ContentValues values = new ContentValues();
values.put(KEY_Answer, answer);
db.update(TABLE_Question, values, where, whereArgs);
insert() inserts a new row. To update a column on an existing row, use update() with a selection like "Ques_id=" + id.

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

Update singel cell of a row in an SQLite database on Android

I wont to update a single cell of a row in the database. However the row contains of 5 columns so and i would like to not passing all the other values as well as they should remain the same.
I have this code snippet:
Cursor cursor = db.query(STATION_TABLE, null, null, null, null, null, null);
//If the database already include some stations.
if(cursor.moveToFirst())
{
ContentValues stationValues = new ContentValues();
for(StopLocation station: stations)
{
stationValues.clear();
//The database already includes the station
if(cursor.getString(POS_STA_ID).equals(station.getId()))
{
values.put(KEY_STA_DISTANCE, "null");
db.update(STATION_TABLE, stationValues, KEY_STA_ID + "=?", new String[]{station.getId()});
}
The db.update method throws this exception:
java.lang.IllegalArgumentException: Empty values
Any ideas on how to solve this?
If I understand the question correctly and assuming you are always dealing with one row, there are two possible ways to approach this:
First:
Get the values of all fields in the entire row, and declare them as content values before updating:
ContentValues cv = new ContentValues();
cv.put("Field1","123");
cv.put("Field2","True")
Second:
Use execSQL() method:
String strSQL = "UPDATE your_table SET Field1 = foo WHERE POST_STA_ID = "+ station.getId();
myDataBase.execSQL(strSQL);

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

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

Increase the value of a record in android/sqlite database

I try to increase the value of an integer key in a row in my table. However, nothing really seems to happen.
db.rawQuery("UPDATE table SET key = key + 1 WHERE name=?", new String[] {name});
However, this code works fine (just sets the key to a hard-coded value):
ContentValues values = new ContentValues();
values.put("key", 2);
db.update("table", values, "name=?", new String[] {name});
Also tried '?' instead of just ?, but it resulted just in a run-time error.
From http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html:
public Cursor rawQuery (String sql, String[] selectionArgs)
Runs the provided SQL and returns a Cursor over the result set. Returns a Cursor object, which is positioned before the first entry
compared to:
public void execSQL (String sql, Object[] bindArgs)
Execute a single SQL statement that is not a query. For example, CREATE TABLE, DELETE, INSERT, etc.
In other words, SQL queries which return a table are to be run with rawQuery, and those that do not return tables are to be run with execSQL.

Categories

Resources