1

I am building an android application where I am using data helper class to store the data.

Now the problem I am facing is How can I delete top old 10 data from table.

I am using below query but my application crashed.

    public void deleteRec() {
    String ALTER_TBL ="delete from " + TBL_NAME +
            " order by _id LIMIT 3);";
    db.execSQL(ALTER_TBL);
    }

And calling using -

new DatabaseHelper(getApplicationContext()).deleteRec();
3
  • 1
    Have you tried to take away the last ")" ? Commented Feb 27, 2015 at 12:46
  • After remove bracket, change the 3 to 10 ;) Commented Feb 27, 2015 at 12:49
  • I updated my answer once check. Commented Feb 27, 2015 at 13:01

3 Answers 3

2

Once try as follows

String ALTER_TBL ="delete from " + TBL_NAME + " where rowid IN (Select rowid from " + TBL_NAME + " limit 10)";

Hope this will helps you.

Sign up to request clarification or add additional context in comments.

Comments

1

DELETE with LIMIT is not enabled in the SQLite library used in Android.

You have to use a subquery to get the IDs:

String ALTER_TBL = "delete from " + TBL_NAME + " where _id in " +
        "(select _id from " + TBL_NAME + " order by _id limit 10)";

1 Comment

I guess this is the real reason it didn't work. Beside the ')' that shouldn't be in the query. DELETE with LIMIT has to be enabled with compile options, see: sqlite.org/compile.html#enable_update_delete_limit
0

I think your problem is the last parentheses:

enter image description here

5 Comments

Thanks I too think that this is the problem can you please suggest me better way to do that
Remove that and try again.If it gives errors, copy it and add it to your question
What I should remove ?
you have to remove the bracket
@hiteshmatnani remove the perenthesis ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.