简体   繁体   English

如何在Android的sqlite数据库中获得5条最后记录?

[英]How to get 5 last record in the sqlite db in android?

Since the db does not have create date and some ordering field (but in my observation the last row is the latest record), 由于数据库没有创建日期和某些排序字段(但据我观察,最后一行是最新记录),

so how can i get the five last record in some condition eg five record that their schoolid == 1? 因此,在某些情况下,我如何才能获得最后的五个记录,例如五个记录的学生标识== 1?

Thanks 谢谢

public Cursor select()
{
    String orderBy = FIELD_pubKey+" DESC";
    Cursor cursor = iReadDatabase.query(TABLE_NAME, null, null, null, null, null, orderBy);
    cursor.moveToFirst();
    return cursor;
}

There's no such thing as a last record or a first or a 42nd. 没有最后一个记录或第一个或第42个记录。

Which records appears last in the result of a query is dependent on the query plan, or an Explicit order by if you add one. 在查询结果中最后出现的记录取决于查询计划,或者取决于显式顺序(如果添加一个)。

Select * From Table Where ... The rows will be returned in whatever order the engine considers suitable at the time. 选择* From Table Where ...将以引擎认为合适的顺序返回行。 If you need them in specific order, then add an order by clause to the query, anything else is asking for it. 如果您需要按特定顺序排列它们,则在查询中添加一个order by子句,其他任何要求。

Something like 就像是

Select * From Table Order by SomeColumn desc limit 5

will do what you require. 将满足您的要求。

Now what column you need to order by I've no idea, but you need one that will do the job, assuming automatic primary key, but note it is possible to mess with that. 现在我不知道您需要订购哪一列,但是您需要一个可以完成此工作的列(假设具有自动主键),但是请注意,有可能将其弄乱。

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

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