Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update first n records in SQLite

Tags:

sqlite

Is there any query available to update only first n records of SQLite DB ..?


2 Answers

The previous answer assumes that the primary key identifier is starting at 1. This would not be the case if rows have been deleted.

http://www.sqlite.org/lang_update.html states: "If SQLite is built with the SQLITE_ENABLE_UPDATE_DELETE_LIMIT compile-time option then the syntax of the UPDATE statement is extended with optional ORDER BY and LIMIT clauses..."

In this case, you can use this simple query:

UPDATE table SET columns = 'value' WHERE 1 LIMIT n ORDER BY identifier ASC

The other clause might not be needed based on the default ordering scheme.

like image 146
raidfive Avatar answered Sep 05 '25 00:09

raidfive


If you are using a sequential int identifier, UPDATE table SET columns = 'value' WHERE identifier <= n

like image 31
Jordan S. Jones Avatar answered Sep 05 '25 01:09

Jordan S. Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!