Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7681169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:16:51+00:00 2026-05-31T18:16:51+00:00

Please let me know how to delete n-rows in android sqlite database. I used

  • 0

Please let me know how to delete n-rows in android sqlite database. I used this code:

   String ALTER_TBL ="delete from " + MYDATABASE_TABLE +
         "where"+KEY_ID+"in (select top 3"+ KEY_ID +"from"+ MYDATABASE_TABLE+"order by _id );";

          sqLiteDatabase.execSQL(ALTER_TBL);

But it shows an error.

03-21 13:19:39.217: INFO/Database(1616): sqlite returned: error code = 1, msg = near "in": syntax error
03-21 13:19:39.226: ERROR/Database(1616): Failure 1 (near "in": syntax error) on 0x23fed8 when preparing 'delete from detail1where_id in (select top 3_idfromdetail1order by _id );'.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:16:52+00:00Added an answer on May 31, 2026 at 6:16 pm
    String ALTER_TBL ="delete from " + MYDATABASE_TABLE +
         " where "+KEY_ID+" in (select "+ KEY_ID +" from "+ MYDATABASE_TABLE+" order by _id LIMIT 3);";
    
    • there is no “top 3” command in sqlite I know of, you have to add a limit
    • watch out for spaces when you add strings together : "delete from" + TABLE + "where" = "delete frommytablewhere"

    This approach uses two steps to delete the first N rows.

    1. Find the first N rows:

      SELECT id_column FROM table_name ORDER BY id_column LIMIT 3

      The result is a list of ids that represent the first N (here: 3) rows. The ORDER BY part is important since SQLite does not guarantee any order without that clause. Without ORDER BY the statement could delete 3 random rows.

    2. Delete any row from the table that matches the list of ids:

      DELETE FROM table_name WHERE id_column IN ( {Result of step 1} )

      If the result from step 1 is empty nothing will happen, if there are less than N rows just these will be deleted.

      It is important to note that the id_column has to be unique, otherwise more than the intended rows will be deleted. In case the column that is used for ordering is not unique the whole statement can be changed to DELETE FROM table_name WHERE unique_column IN (SELECT unique_column FROM table_name ORDER BY sort_column LIMIT 3). Hint: SQLite’s ROWID is a good candidate for unique_column when deleting on tables (may not work when deleting on views – not sure here).

    To delete the last N rows the sort order has to be reversed to descending (DESC):

    DELETE FROM table_name WHERE unique_column IN (
        SELECT unique_column FROM table_name ORDER BY sort_column DESC LIMIT 3
      )
    

    To delete the Nth to Mth row the LIMIT clause can be extended by an OFFSET. Example below would skip the first 2 rows and return / delete the next 3.

    SELECT unique_column FROM table_name ORDER BY sort_column LIMIT 3 OFFSET 2
    

    Setting the LIMIT to a negative value (e.g. LIMIT -1 OFFSET 2) would return all rows besides the first 2 resulting in deletion of everything but the first 2 rows – that could also be accomplished by turning the SELECT .. WHERE .. IN () into SELECT .. WHERE .. NOT IN ()


    SQLite has an option to enable the ORDER BY x LIMIT n part directly in the DELETE statement without a sub-query. That option is not enabled on Android and can’t be activated but this might be of interest to people using SQLite on other systems:

     DELETE FROM table_name ORDER BY sort_column LIMIT 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please let me know if the bellow code is correct. I mean connection is
Possible Duplicate: Javascript cookie delete Please let me know how to delete specific cookie
please let me know a java multithread application which i can refer ( source
Please let me know the difference between ~ and ! operator in java.
Please let me know what is the best way to determine composition of young
Please let me know if you have any idea about it. Thanks EDIT What
Please let me know how to get the client IP address, I have tried
Please Let me know if any best approach available for these two SSIS Activities.
Please let me know whether its correct or not? I am mapping the whole
Can somebody please let me know how we can determine if a local disk

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.