Aijaz Ali
Aijaz Ali

Reputation: 403

how to get last 10 added records in sqlite table in iphone

I have table view in which I want to select last 10 records added in query. I am using following query which return all the records but I want last 10 added.

here is the query

NSString* select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster where ContentAddedByUserID='%@' AND HiveletCode='%@'",appDelegate.userID,appDelegate.organizationCode];

I have searched some where that we can get by top records like this I get that we can fetch using date between two dates any idea how to get without date last 10 records.

I want to fetch all data which is last 10 added.

Upvotes: 6

Views: 10118

Answers (2)

Deepak Bhatia
Deepak Bhatia

Reputation: 6276

According to documentation, each row in the Sqlite contains a 64 Bit auto incremented id associated with it.
So you can use query as

SELECT * FROM ContentMaster WHERE ContentAddedByUserID='%@' AND HiveletCode='%@'
ORDER BY ROWID DESC
LIMIT 10

Remember,

  1. If you declare an ordinary table column to use one of the defined special names ROWID, _ROWID_, or OID, then the use of that name will refer to the declared column not to the internal ROWID.
  2. If a table contains a column of type INTEGER PRIMARY KEY, then that column becomes an alias for the ROWID.

Upvotes: 4

exepti0n
exepti0n

Reputation: 611

Every row in a table has an unique ID, which you can use for your query:

SELECT
    *
FROM
    ContentMaster
WHERE
    ContentAddedByUserID='%@'
AND
    HiveletCode='%@'
ORDER BY
    rowid DESC
LIMIT 10

Upvotes: 12

Related Questions