简体   繁体   English

如何在iPhone的sqlite表中获取最后10条添加的记录

[英]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. 我有一个表视图,我想选择查询中添加的最后10条记录。 I am using following query which return all the records but I want last 10 added. 我正在使用以下查询,该查询返回所有记录,但我想添加最后10个。

here is the query 这是查询

NSString* select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster where ContentAddedByUserID='%@' AND HiveletCode='%@'",appDelegate.userID,appDelegate.organizationCode]; NSString * select = [[NSString alloc] initWithFormat:@“ select * FROM ContentMaster,其中ContentAddedByUserID ='%@'和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. 我已经搜索了一些我们可以从顶级记录中获取的信息,例如,我可以使用两个日期之间的日期获取任何想法,无论如何获取没有日期的最近10条记录。

I want to fetch all data which is last 10 added. 我想获取最近添加的所有数据。

Every row in a table has an unique ID , which you can use for your query: 表格中的每一行都有一个唯一的ID ,您可以将其用于查询:

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

According to documentation , each row in the Sqlite contains a 64 Bit auto incremented id associated with it. 根据文档 ,Sqlite中的每一行都包含一个与之关联的64位自动递增ID。
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 . 如果您声明普通表列使用已定义的特殊名称ROWID, _ROWID_, or OID ,则使用该名称将引用已声明的列而不是内部ROWID
  2. If a table contains a column of type INTEGER PRIMARY KEY, then that column becomes an alias for the ROWID . 如果表包含INTEGER PRIMARY KEY类型的列,则该列将成为ROWID的别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM