Android – ormlite – returns the item w / maximum ID (or value)

I'm using ormlite w / Android. I want to get the ID value of the object with the highest ID value

I would also like to summarize this as a single object with a maximum / minimum value requesting any field. What is the best practice for doing so?

I realized that I could execute an original query, but is there any other way to jump out? When executing fetchrequests in coredata on IOS, I was imagining something similar to predictions

resolvent:

Your answer looks good @ Ben, but I think I will provide some additional information for future generations:

>If you know that the limit (1) will return a single result, you can use queryforfirst

qBuilder.orderby("my_field_column_name", false);
qBuilder.limit(1L);
MyEntity maxEntity = myEntityDao.queryForFirst(qBuilder.prepare());

>The original query is OK. It involves more objects and checks, but it is cheaper if the table size is large, because you don't need to sort

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]);

Obviously, if your columns are of different types, you will get a different object from result [0], and you must convert it to the appropriate type according to the object

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>