how to use of update operation sqlite? - android

I have problem with my sqlite update operation, please help me how to fix it
that is my database handler for update data :
public int updateAccountCalc(Accounts accounts) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, accounts.getId());
values.put(KEY_NAMA, accounts.getNama());
values.put(KEY_EMAIL, accounts.getEmail());
values.put(KEY_BB0, accounts.getBB0());
values.put(KEY_BB1, accounts.getBB1());
values.put(KEY_BB2, accounts.getBB2());
values.put(KEY_BB3, accounts.getBB3());
values.put(KEY_LILA, accounts.getLila());
// updating row
return db.update(TABLE_NAME, values, KEY_ID + " = ?",
new String[] { String.valueOf(accounts.getId()) });
}
and this is code to use it :
db.updateAccountCalc(new Accounts(0, nama.toString(), email.toString(), bb, 0, 0, 0, lila));
i have inserted value of bb, but is never updated on sqlite database

Related

update column with different values sqlite database android studio

I have a hidden database with 7 columns, one of which is a price and whose values ​​are null. I want to enter some value in the field column for id = 1, then for id = 2 I enter another value etc. and when I fill in the price column I want to update the whole. Is that possible?
My code updates only one value
public boolean updateData(String id,String name, String number_phone, String about, Integer price, String color){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, id);
contentValues.put(COL_2, name);
contentValues.put(COL_3, number_phone);
contentValues.put(COL_4, about);
contentValues.put(COL_5, price);
contentValues.put(COL_6, color);
db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{id});
return true;
}
I don't quite get your question but from what i understand this could might help you out.
If you want only one field values to be updated then you need to use for loop.
In onCreate method call this with the array of prices that you want to be updated in table's each row.
Array of prices should not be greater than no. of rows exist in table.
for(int i=0;i<(no. of rows in table);i++){
updateData(rowId,priceArray[i]);
}
Now the update method will be like.
public boolean updateData(String id, Integer price){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_5, price);
db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{id});
return true;
}
If you want to update certain rows for example {1,4,5} then you have to update it one by one by calling updateData method with id and prices .
But if you want same value of price field to be updated in certain rows like above {1,4,5} than you can
public boolean updateData(Integer price){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_5, price);
db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{1,4,5});
return true;
}

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

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

How to keep the database updated when the user enters new values in android studio?

I'm dealing with a simple app, which basically stores various dates. But when I enter a new date, I have to close the app and open it again. The same thing counts for changing the date values. I can only see the changes by closing the app and reopening it.
Here are the two main functions in my DB class;
public void addBirthday(Person person){ //Add a data to database
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(column_name, person.getName());
cv.put(column_sname, person.getSname());
cv.put(column_day, person.getDay());
cv.put(column_month, person.getMonth());
cv.put(column_year, person.getYear());
db.insert(TABLE_NAME, null, cv);
db.close();
}
public int updateBirthday(Person person) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues cv = new ContentValues();
cv.put(column_name, person.getName());
cv.put(column_sname, person.getSname());
cv.put(column_day, person.getDay());
cv.put(column_month, person.getMonth());
cv.put(column_year, person.getYear());
// 3. updating row
int i = db.update(TABLE_NAME, //table
cv, // column/value
column_id +" = ?", // selections
new String[] { String.valueOf(person.getId()) }); //selection args
// 4. close
db.close();
return i;
}
You will need to refresh the view (probably a ListView) where you display this data.
Get rid of the notifyDataSetChanged() and call requery() on your Cursor

Updating a data base with a single field

I'm currently developing an Android app. The database has 1 column only named content. Here it is the code:
public long insert(String content){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
How to make an update for a specific column (according to its index) in that database?
db.update
(
MYDATABASE_TABLE, contentValues , ID_FIELD_CONST + " = ?",
new String[] { idValue }
);
You can use:
ContentValues cv = new ContentValues();
cv.put(KEY1,value1);
cv.put(KEY2,value2);
//... all the fields you want to update
String where = KEY_CONTENT + " = " + value;
db.update(table_name,cv,where,null);
KEY1,KEY2 ... are the name of the fields you want to update
value1,value2... are the new values of the fields you want to update
the where String is to tell in which row you need to update.

Categories

Resources