简体   繁体   中英

No error but data is not getting deleted from Sqlite database using delete

I am trying to delete the data from table and was able to write the command but unfortunately data is not getting deleted.. There is no error or exception reported but nothing is happening.

Checked the similar posts in stackoverflow but nothing is helping me and unable to understand where I am going wrong.

Below is my code

Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -0);
backup_date=((c.get(Calendar.DATE)-1)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.YEAR));

System.out.println(backup_date);
                Database.getInstance(getApplicationContext()).getWritableDatabase().beginTransaction();
                result=Database.getInstance(getApplicationContext()).getWritableDatabase().delete(tablename, column2 + " < '" + backup_date + "'", null);
                Database.getInstance(getApplicationContext()).getWritableDatabase().setTransactionSuccessful();
                if(result!=1){
                    Toast.makeText(getApplicationContext(), "Problem with deletion try again", Toast.LENGTH_LONG).show();
                }
                else{
                    System.out.println("deleted successfully");
                    Toast.makeText(getApplicationContext(), "Data deleted successfully for the date " + backup_date, Toast.LENGTH_LONG).show();
                }

Where am I going wrong?

It looks like you didn't call endTransaction .

Also, this probably isn't your main problem, but your logic for calculating the date won't work in December or on the first day of a month (and possibly other times too). Rather than trying to manipulate the individual date fields separately, make a new variable with the original date, then add a month and subtract a day. Then work with that date. Trying to adjust individual fields in a vacuum is never going to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM