Solution 1 :
You have to call close function on your database after updation/deletion to commit the changes.
db.close()
Problem :
i have been working on this app which uses sqlite, and i want to update and delete rows when the function is called, the primary key is passed as a parameter in the function. But when the function is called no updates are seen in the table. I am unable to identify where I am going wrong and whats the error, please help.
//create table
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE registerJob (JID INTEGER PRIMARY KEY AUTOINCREMENT, JobProfile VARCHAR, Organisation VARCHAR, Location VARCHAR, AboutOrg VARCHAR, AboutJob VARCHAR, Incentives VARCHAR , Salary INTERGER, Number INTEGER, Category Varchar, Duration number,Twitter Varchar, Facebook Varchar, LinkedIn Varchar)");
}
//update vacancy
public void updateVacancy(String orgName,String aboutOrg, String jp, String l, String aoj, int s, String i, int n, int jid) {
try {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("JobProfile", jp);
contentValues.put("Organisation", orgName);
contentValues.put("Location", l);
contentValues.put("AboutOrg", aboutOrg);
contentValues.put("AboutJob", aoj);
contentValues.put("Incentives", i);
contentValues.put("Salary", s);
contentValues.put("Number", n);
db.update("registerJob",contentValues," JID= "+jid,null);
Toast.makeText(contexto, "Data updated", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(contexto, "DATABASE HELPER: Update vacancy "+e, Toast.LENGTH_SHORT).show();
}
}
//delete vacancy
public void deleteJob(int jid) {
try{
SQLiteDatabase db = this.getWritableDatabase();
db.delete("registerJob"," JID= "+jid,null);
}catch (Exception e){
Toast.makeText(contexto,"DATABASE HELPER deleteJob "+e,Toast.LENGTH_SHORT).show();
}
}
Comments
Comment posted by Gautam
what error are you getting? and how are you checking if an update is a success or fail?
Comment posted by Payal Soni
I am getting no error message, its just that the data is not getting updated or deleted
Comment posted by Payal Soni
and to check the success of update I have an activity to display the updated data
Comment posted by Gautam
the code is correct now you have to debug where it’s failing. It might be the schema or the data which can cause an error. Try running test command like db.execSQL(“UPDATE registerJob SET JobProfile =’testing’ WHERE id=1”) and check if this is reflected or not.
Comment posted by Alpha 1
I can’t see any issue in the code. Are you getting any exception? Can you please check if table has data before updation or deletion?
Comment posted by Payal Soni
There are no exceptions also the data is getting saved in the table.
Comment posted by Alpha 1
It’s weird. No idea why it is happening? Try calling this.close() instead of db.close(). this.close(). Is is possible for you to share the code on GitHub or through any other means?