ORMLite - 退货项目瓦特/最大ID(或价值) [英] ORMLite - return item w/ maximum ID (or value)
问题描述
我使用ORMLite瓦特/ Android系统。我想抓住对象的ID值w /最高值ID。
I'm using ORMLite w/ Android. I want to grab the ID value of the object w/ the highest-valued ID.
我也想推广这要求单个对象W /最大/最小值的任意字段。什么是最好的做法这样做呢?
I'd also like to generalize this to requesting a single object w/ max/min value for any arbitrary field. What is the best practice for doing this?
我知道我可以执行原始查询,但那里跳了出来,在任何人的任何其他方式?我想象类似iOS上执行的的coredata FetchRequests时predicates东西。
I realize I could execute a raw query, but is there any other way that jumps out at anyone? I'm imagining something similar to Predicates when executing FetchRequests in CoreData on iOS.
推荐答案
您的答案看起来不错@Ben但我想我会放弃给后人一些额外的信息:
Your answer looks good @Ben but I thought I'd give some extra info for posterity:
-
您可以使用
queryForFirst
如果你知道你的限制(1)
将返回一个结果
You could use a
queryForFirst
if you know yourlimit(1)
will return a single result.
qBuilder.orderby("my_field_column_name", false);
qBuilder.limit(1L);
MyEntity maxEntity = myEntityDao.queryForFirst(qBuilder.prepare());
一个原始查询也将工作。它涉及多个对象和检查,但将是,如果表的大小是大便宜,因为你不需要做排序。
A raw query would also work. It involves more objects and checking but will be cheaper if the table size is large since you don't need to do the ordering.
qBuilder.selectRaw("MAX(my_field_column_name)");
GenericRawResults<String[]> results =
myEntityDao.queryRaw(qBuilder.prepareStatementString());
String[] result = results.getResults().next();
int max = Integer.parseInt(result[0]);
显然,如果你的列是一个不同的类型,那么你会得到一个不同的对象出了结果[0]
,你将不得不转换成根据相应的类型对象
Obviously, if your column was a different type then you would get a different object out of result[0]
that you would have to convert into the appropriate type depending on the object.
这篇关于ORMLite - 退货项目瓦特/最大ID(或价值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!