Record is not getting update in database - android

I have saved data in database. I am trying to update data.
But record is not getting update.
If I debug the update function it shows the values i have entered. But when I retrieve the data it dose not show updated values.
What's wrong?
update function in helper class
public int updateEvent(EventData event) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE,event.getTitle());
values.put(KEY_FROM_DATE,event.getFromDate());
values.put(KEY_TO_DATE,event.getToDate());
values.put(KEY_DAY_OF_WEEK,event.getDayOfWeek());
values.put(KEY_LOCATION,event.getLocation());
values.put(KEY_NOTIFICATION_TIME,event.getNotificationTime());
// updating row
return db.update(TABLE, values, KEY_ID + " = ?",
new String[] { String.valueOf(event.getId()) });
}
updating in activity:
if (editMode) {
eventData.setTitle(title.getText().toString());
eventData.setFromDate(showFromTime.getText().toString());
eventData.setToDate(showToTime.getText().toString());
eventData.setDayOfWeek(selectDay.getText().toString());
eventData.setLocation(mAutocompleteView.getText().toString());
eventData.setNotificationTime(notifyTime.getText().toString());
db.updateEvent(eventData);
}
else {
db.addEvent(new EventData(eventTitle, startTime, endTime, dayOfWeek, location, notificationTime));
}
setResult(RESULT_OK, i);
finish();
}
Thank you..

Related

how to update a SQQLite database with particular row?

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.

Can anyone explain how this code INSERTS data into a database?

Can someone explain to me how this code works and how I am inserting this data into the database? I Java two Java files. The first piece of code is located in the first Java file and the second is located in the second Java file. Why am I passing the following parameters ImageID, currentDay, v.getID. Then when I call the insertNewRoutine function it is using the parameters int activityResourceID, String day and int slot? Confused about why it's using that?Thanks in advance!
boolean routineInserted = myDb.insertNewRoutine(ImageID, currentDay, v.getId());
if (routineInserted == true) {
Toast.makeText(MondayRoutineEdit.this, "Activity Inserted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MondayRoutineEdit.this, "Activity Not Inserted", Toast.LENGTH_LONG).show();
}
public boolean insertNewRoutine(int activityResourceID, String day, int slot)
{
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + RoutineTable + " WHERE DayID ='" + day + "' AND SlotID =" + slot);
ContentValues contentValues = new ContentValues();
contentValues.put(RoutineColumn2, day);
contentValues.put(RoutineColumn3, activityResourceID);
contentValues.put(RoutineColumn4, slot);
long result = db.insert(RoutineTable, null, contentValues);
if (result == -1)
return false;
else
return true;
}
ContentValues contentValues = new ContentValues();
contentValues.put(RoutineColumn2, day);
contentValues.put(RoutineColumn3, activityResourceID);
contentValues.put(RoutineColumn4, slot);
long result = db.insert(RoutineTable, null, contentValues);
You are population the content values (they are going to be inserted as VALUES in sql) with function paramaters.
This method call is responsible for insertion.
SQLiteDatabase.class exposes methods to manage a SQLite database.
Reference to the official android documentation
https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert(java.lang.String,%20java.lang.String,%20android.content.ContentValues)

how to use of update operation sqlite?

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

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

Android multiple record SQLite

I saw in the SQLite documentation that you can not insert multiple records at the same time. For example, I have 2 TEXT fields and at the same time I want to insert in filed1 10 records with the date that goes from today to 10 days, and in the field2 value of 50 was added in the 10 record. I hope I explained.
Now normally I insert the record in this way:
public void insertDb(View v) {
ContentValues cv = new ContentValues();
cv.put(Table.ONE, mNe.getText().toString());
cv.put(Table.ONE, mNel.getText().toString());
...
for that you have to use for loop or anything
like below code
public void add_device(String data,
ArrayList<HashMap<String, String>> jsonlist) {
try {
database = this.getWritableDatabase();
for (HashMap<String, String> map : jsonlist) {
ContentValues values = new ContentValues();
values.put(SAVE_COLUMN_NAME, data);
values.put(SAVE_COLUMN_KEY, map.get(SAVE_COLUMN_KEY));
values.put(SAVE_COLUMN_VALUE, map.get(SAVE_COLUMN_VALUE));
Long int1 = database.insertOrThrow(SAVE_TABLE_NAME, null,
values);
Log.i("inserted value", values + "");
Log.i("insserted value ", int1 + "");
}
}
catch (Exception e) {
}
}
you can use loop like i have used

Categories

Resources