Is there an easy way to simulate check behabiour in sqlite? - android

I need to keep data from checkboxes in database.
Is there an easy way to change value of row i to 1 and values of all other rows to 0? Or I have to iterate over all rows?
I also sow such solution:
ContentValues values = new ContentValues();
values.put(DragViewSyncTable.COLUMN_PLAYER_PREPARED, 0);
context.getContentResolver().update(DragViewSyncContentProvider.CONTENT_URI, values, null, null);
values = new ContentValues();
values.put(DragViewSyncTable.COLUMN_PLAYER_PREPARED, 1);
context.getContentResolver().update(DragViewSyncContentProvider.CONTENT_URI, values, DragViewSyncTable.COLUMN_ID + " =? ", new String[]{""+i});

If you have the row's primary key (I'll use the rowid), you can run the equivalent to the following query:
UPDATE table SET checked = (rowid == ?)

Related

Is there a way to change android sqlite data?

I want to swap sqlite data
now, I am changing data after saving it in a variable
Is there a better way? Thank you
updateDbDate(fromItem.getTitle(),fromItem.getCable(),fromItem.getImg(),toItem.getId());
updateDbDate(toItem.getTitle(),toItem.getCable(), toItem.getImg(),fromItem.getId());
public void updateDbDate(String mTitle, String mCable, String mImg, int id)
{
ContentValues values = new ContentValues();
values.put("title", mTitle);
values.put("cable", mCable);
values.put("bimg", mImg);
db.update(Define.DB, values,"_id = " + "'" + id + "'",null);
}
What I understand is that you want to swap the values of 3 columns between 2 rows for which you know the ids.
If you also know the values of the other 3 columns then your method is correct.
If you don't know them and you have to read them from the table then I think it would be better to just swap the ids:
public void swapIds(int id1, int id2) {
ContentValues values = new ContentValues();
values.put("_id", -1);
db.update(Define.DB, values, "_id = ?", new String[] {String.valueOf(id1)});
values.put("_id", id1);
db.update(Define.DB, values, "_id = ?", new String[] {String.valueOf(id2)});
values.put("_id", id2);
db.update(Define.DB, values, "_id = ?", new String[] {"-1"});
db.close();
}
The 1st update sets temporarily -1 to the first _id, because I guess it is the PRIMARY KEY and it can't be set to id2 because it already exists.
The 2nd update sets id1 to the second _id (it can be done since id1 does not exist in the table from the previous update).
And the 3d update sets id2 to the first _id.
Now you can call the method:
swapIds(100, 200);
Note: this method is not recommended if there are references in other tables to the _id of this table.

Updating rows in Android SQlite

I'm using SQlite in my Android app and the task is - how can I update all the rows in one table?
I have a 1st column with name "cl_id" (integer numbers 1-2-3-4..) and after deleting some rows, I wan't to make a cycle to fill this 1st column with a new values to keep them in right order.
I was trying to execute:
data.put("cl_id",index);
db.update("mdb_table_contactList", data, null, null);
but it's updates all the values in the first column :(
To update the field in specific row, try this;
ContentValues data = new ContentValues();
data.put("cl_id",index);
db.update("mdb_table_contactList", data, "id="+_id, null);
db.close();
It's good practice to have a primary key for each row data.
Here _id would be your primary key.
"just give rowId and type of data that is going to be update in ContentValues."
public void updateStatus(String id , int status){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues data = new ContentValues();
data.put("status", status);
db.update(TableName, data, "columnName" + " = "+id , null);
}
Try this it must work
void updateDatail(String id) {
ContentValues data = new ContentValues();
data.put("cl_id",index);
db.update("mdb_table_contactList", data, "id=?", new String[]{id});
db.close();
}

How can use sqlite update via ContentValues when want to update depend on current value and how can retrieve the Result?

I've a situation in sqlite that make you an example: My table has two fields,"_id" and "_score" . I have a record with _id=1, _score=10. I want to update this row to 5 number more than the current value(10). in SQL i can do it simple like:
Update my_table set _score = _score + 5 where _id = 1
but in sqlite I have these that I don't know how can fix it to what I want :
ContentValues values = new ContentValues();
values.put("_score", my_value);
SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
int id = db.update(MY_TABLE,values,"_id = ?",new String[]{String.valueOf(my_id)});
and the Other problem is the returned value. I think in above example I give
id = 1
for 1 row effected. but I want to know that: Is there any way to retrieve the value of updated column(in my example I want to give "15"). Some thing like in SQL server that we fetch from
"##ROWCOUNT" or "##fetch_status"
Hope to describe it well. thanks.
Android's update() function can set only fixed values.
To execute your SQL statement, you have to use execSQL():
db.execSQL("Update my_table set _score = _score + 5 where _id = 1");
In SQLite, the UPDATE statement does not return any data (except the count of affected rows). To get the old or new data, you have to execute a SELECT separately.
For your first problem about updating the score value try this:
ContentValues values = new ContentValues();
values.put("_score", my_value);
SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
int id = db.update(my_table, values, _id + " = ?",
new String[] { String.valueOf(my_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 update the table values

I have the table(shown in fig). I want to update the time column. I have the medicine_id(second column) how to update the different times depend upon the same medicine_id(second column).
Also, I have four rows in my DB when I updated time it should be two row means, I want to remove the other two rows with the particular medicine_id..
I have the following code for update:
if(mon) {
for (int i = 0; i < dosage_per_day; i++) {
mMedicineDbHelper.updateMedicines(new
MedicineTime(row_id, time_InMillies[i]));
}
}
My DB code:
public void updateMedicines(MedicineTime medicineTime) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ID, medicineTime.getId());
values.put(MEDICINEID, medicineTime.getMedicine_id());
values.put(TIME, medicineTime.getTime_in_millis());
db.update(TABLENAME, values, MEDICINEID + " = ?",
new String[]{String.valueOf(medicineTime.getMedicine_id())});
db.close();
}
It shows exception..
anybody help to solve this...
For the original question asked you're executing the following code:
db.update(TABLENAME, values, MEDICINEID + " = ?",
new String[]{String.valueOf(medicineTime.getMedicine_id())});
For some reason on Android, converting numeric values to Strings and using them for selection args never works properly. See: Android Sqlite selection args[] with int values. The best way to do this is the following:
db.update(TABLENAME, values, MEDICINEID + " = " + medicineTime.getMedicine_id(), null);

Categories

Resources