Delete few rows from db table

user1810931

Right now in my code I check if row count is greater than 100 then I delete all the rows by using context.getContentResolver().delete( tablename, null, null );.

But I want to delete rows from 1 to 80 and after deleting those rows, offset( rename ) the last left over rows from 80 to 100 -> 1 to 20 and if I add any rows in that table the primary key ID should start from 21.

Can someone help me with this?

NigelK

Assuming the primary key of your table is called _id and is set to autoincrement. Delete the 80 rows you don't want:

String query = "DELETE FROM " + tablename + " WHERE _id <= 80";
context.getContentResolver().execSQL(query);

For those rows that remain (that are ids 81 - 100), update the id by subtracting 80 so they'll then go from 1 - 20:

query = "UPDATE " + tablename + " SET _id = _id - 80";
context.getContentResolver().execSQL(query);

Update the entry in the sqlite_sequence table for this table. Set seq to that of the last id on the table:

query = "UPDATE sqlite_sequence SET seq = 20 WHERE name = '" + tablename + "'";
context.getContentResolver().execSQL(query);

When you next insert, _id will continue from 21.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delete rows from table

From Dev

Delete X rows from table

From Dev

Retrieve all rows from table except few rows in laravel

From Dev

DB2: Can not delete rows from empty table after it was referenced in foreign key

From Dev

DB2: Can not delete rows from empty table after it was referenced in foreign key

From Dev

Oracle how to delete from a table except few partitions data

From Java

Select few rows from a result table and achieve below output

From Dev

Delete duplicate rows from table with no unique key

From Dev

Better "delete rows from table" performance

From Dev

Delete Rows from a Constrained Reference Table

From Dev

delete rows from a table using MySQL Scheduler

From Dev

Delete rows from table recursively using JQ

From Dev

Delete duplicate rows from a BigQuery table

From Dev

Cannot delete rows from a table SQL

From Dev

multiple queries to delete rows from table

From Dev

delete rows from a table that are not present in another

From Dev

How to delete some similar rows from a table?

From Dev

mysql delete duplicate rows from table

From Dev

Delete rows from Table, remove unpaid transaction

From Dev

Delete rows from table inner join

From Dev

How to delete from table then delete what deleted rows referenced? (postgresql)

From Dev

How to Use Delete Triggers to Delete Rows From Same Table?

From Dev

Delete all but top 50 rows from DB with CodeIgniter

From Dev

Delete many rows from a large percona mysql db

From Dev

Delete rows from a data table that exists in another data table

From Dev

Delete rows from a data table that exists in another data table

From Dev

How to delete rows from a table based on a join with another table?

From Dev

How to delete rows from one table matching another table?

From Dev

Delete all data rows from an Excel table (apart from the first)

Related Related

  1. 1

    Delete rows from table

  2. 2

    Delete X rows from table

  3. 3

    Retrieve all rows from table except few rows in laravel

  4. 4

    DB2: Can not delete rows from empty table after it was referenced in foreign key

  5. 5

    DB2: Can not delete rows from empty table after it was referenced in foreign key

  6. 6

    Oracle how to delete from a table except few partitions data

  7. 7

    Select few rows from a result table and achieve below output

  8. 8

    Delete duplicate rows from table with no unique key

  9. 9

    Better "delete rows from table" performance

  10. 10

    Delete Rows from a Constrained Reference Table

  11. 11

    delete rows from a table using MySQL Scheduler

  12. 12

    Delete rows from table recursively using JQ

  13. 13

    Delete duplicate rows from a BigQuery table

  14. 14

    Cannot delete rows from a table SQL

  15. 15

    multiple queries to delete rows from table

  16. 16

    delete rows from a table that are not present in another

  17. 17

    How to delete some similar rows from a table?

  18. 18

    mysql delete duplicate rows from table

  19. 19

    Delete rows from Table, remove unpaid transaction

  20. 20

    Delete rows from table inner join

  21. 21

    How to delete from table then delete what deleted rows referenced? (postgresql)

  22. 22

    How to Use Delete Triggers to Delete Rows From Same Table?

  23. 23

    Delete all but top 50 rows from DB with CodeIgniter

  24. 24

    Delete many rows from a large percona mysql db

  25. 25

    Delete rows from a data table that exists in another data table

  26. 26

    Delete rows from a data table that exists in another data table

  27. 27

    How to delete rows from a table based on a join with another table?

  28. 28

    How to delete rows from one table matching another table?

  29. 29

    Delete all data rows from an Excel table (apart from the first)

HotTag

Archive