how to update a SQQLite database with particular row? - android

while updating in database, I'm not getting the id of particular row
here is my code for update Note in database
public int updateNote(int id, String tittle, String content , String update_at)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Note.COLUMN_TITTLE,tittle);
values.put(Note.COLUMN_CONTENT,content);
values.put(Note.COLUMN_TITTLE,update_at);
return db.update(Note.TABLE_NAME,values,Note.COLUMN_ID+"="+id,null);
}
Please help me.

update returns the number of rows affected and not the id of the row.
In your case if successful it would return 1.
About the id you need you already have it.

As mTak outlined, the update method returns the number of rows affected. You can also see that in the documentation.
If you want your method to return the ID of the row you updated, then return the id that you pass in, since that's how you select the row to update in the first place:
public int updateNote(int id, String tittle, String content , String update_at) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Note.COLUMN_TITTLE,tittle);
values.put(Note.COLUMN_CONTENT,content);
values.put(Note.COLUMN_TITTLE,update_at);
db.update(Note.TABLE_NAME,values,Note.COLUMN_ID+"="+id,null);
return id;
}

In your Activity Class:
//*Note is your setterClass
//*notesList is your ArrayList or list
//*position refers your current list position
//*setNote is a method in Note Class
private void updateNote(String note, int position) {
Note n = notesList.get(position);
// updating note text
n.setNote(note);
// updating note in db
db.updateNote(n);
// refreshing the list
notesList.set(position, n);
mAdapter.notifyItemChanged(position);
}
In your database class try below code:
public int updateNote(Note note) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Note.COLUMN_NOTE, note.getNote());
// updating row
return db.update(Note.TABLE_NAME, values, Note.COLUMN_ID + " = ?",
new String[]{String.valueOf(note.getId())});
}
here i am trying to update single string.i hope this is workig to you.Let me know is this working or not.

Related

Sqlite updating a record

This is my first time meddling with sqlite on android.
Followed a tutorial and created a SQliteHelper Class, there is this method inside:
// Updating single profile
public int updateProfile(Profile profile) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put("cpf", profile.getCpf()); // get cpf
values.put("nome", profile.getNome()); // get nome
values.put("phone", profile.getPhone()); // get nome
// 3. updating row
int i = db.update(TABLE_PROFILES, //table
values, // column/value
KEY_ID+" = ?", // selections
new String[] { String.valueOf(profile.getId()) }); //selection args
// 4. close
db.close();
return i;
}
However, I have no idea how to use it..
I know that in order to get the profile I do the following:
MySQLiteHelper db = new MySQLiteHelper(view.getContext());
db.updateProfile(db.getProfile(0));
But how do I actually update it?
That code updates the profile with an specific id in this part:
// 3. updating row
int i = db.update(TABLE_PROFILES, //table
values, // column/value
KEY_ID+" = ?", // selections
new String[] { String.valueOf(profile.getId()) }); //selection args
So, this id comes from the Profile object you have just pased to the function.
You should do something like:
// get a profile
MySQLiteHelper db = new MySQLiteHelper(view.getContext());
Profile p = db.getProfile(0);
// change something
p.setPhone(55598789);
// update profile p
updateProfile(p);

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

update SQLite database android

Hello i have MySQLite database in my android project and Im trying to update one of the coloume and it doesn't work
my code:
db source:
public void updateToken(int id, int tokens)
{
List<User> users = getAllUsers();
dbHelper.updateToken(database, id, users.get(id).getUsername(),users.get(id).getPassword(), 10);
}
SQLite helper:
epublic void updateToken(SQLiteDatabase db,int id, String username,String password,int newToken) {
ContentValues cv = new ContentValues();
cv.put(COLUMN_USERNAME, username);
cv.put(COLUMN_PASSWORD, password);
cv.put(COLUMN_TOKENS, newToken);
db.update(TABLE_USERS,cv,"_id "+"="+id,null);
}
calling the method :
dataSource.updateToken(userID,10);
As see here SQLiteDatabase.update takes whereArgs as last parameter.
So,instead of passing last param null in update method pass id value according to which want to update row value like:
String[] whereArgs = new String[] {String.valueOf(id)};
db.update(TABLE_USERS,cv,"_id=?",whereArgs);

accessing variable in activity from database

i have an interplay between the MainActivity and the DatabaseAdapter class where I'm trying to access an integer variable from the MainActivity. The code as follows;
in MainActivity...
int clam = 7;
public void onClick_UpdateJ(View v) {
myDb.updateJ();
}
in DbAdapter
public class {
.....
public void updateJ(){
int grape = "coli = ?;
ContentValues arges = new ContentValues();
arges.put(coli, 7);
// Insert it into the database.
db.update(DATABASE_TABLE, arges, grape, new int[]{1});
}
}
As you can see, I'm trying to update the coli data (integer) in the database from the current 1 to 7. How do I do this? I tried shifting everything in DbAdapter to MainActivity but the 'update' method is not recognized.
Thanks in advance.
public void updateJ(int value) {
ContentValues cv = new ContentValues();
cv.put("coli", value);
String where = "coli = ?";
whereArgs = new String[] { Integer.toString(1) };
db.update(DATABASE_TABLE, cv, where, whereArgs);
}
The update method takes 4 arguments:
table (String): the table name
values (ContentValues): the values to insert
selection (String): a SQLite WHERE clause. You already know that the ? character will be replaced by values in selectionArgs.
selectionArgs (String[]): values that replace ?s in the where clause

Trying to update values of an SQLite database from an Android app

Using the following code I can't upgrade the data in the database:
public void modification(int pos) {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,
"administracion", null, 1);
SQLiteDatabase db = admin.getWritableDatabase();
ContentValues record = new ContentValues();
record.put(AdminSQLiteOpenHelper.ALARM_HOUR, alarmList.get(pos).getHour());
record.put(AdminSQLiteOpenHelper.ALARM_DATE, alarmList.get(pos).getDate());
record.put(AdminSQLiteOpenHelper.ALARM_ACTIVE, 1);
int numOfRows = db.update("alarm", record, "id=" + alarmList.get(pos).getId(), null);
db.close();
}
The method update is returning 0 when it should return the number of rows affected and I have already checked that the id I put in the method exists in the database. It should return 1.
¿Does anyone know where is my mistake?
Thanks in advance

Categories

Resources