SQL MAX-MIN in ORMLITE - ANDROID

不羁的心 提交于 2019-12-10 10:41:50

问题


I want to know how can i use MAX MIN command with ORMLITE.

For example lets say we have this table

Table Name = Example
Column 1 = id
Column 2 = name

In ORMLITE how can i get max id ? I looked here but i didnt't understand exactly..

Can someone show me example about Max min in ORMLITE ?


回答1:


QueryBuilder<Account, Integer> qb = accountDao.queryBuilder();

qb.selectRaw("MIN(orderCount)", "MAX(orderCount)");

// the results will contain 2 string values for the min and max

results = accountDao.queryRaw(qb.prepareStatementString());

String[] values = results.getFirstResult();

I found this from documentation




回答2:


This is how I query for max ID in my code:

QueryBuilder<Example, String> builder = dao.queryBuilder();
builder.orderBy("id", false);  // true or false for ascending so change to true to get min id
Example example = dao.queryForFirst(builder.prepare());
String id = null;
if (example == null)
    id = "-1";
else
    id = example.getId();

A couple of alternative answers can also be found here: ORMLite - return item w/ maximum ID (or value)




回答3:


You can use:

dao.queryRawValue("select MAX(columnName) from tableName")

It will directly return long value.

refer: http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_5.html#DAO-Methods

queryRawValue(String query, String... arguments)

Perform a raw query that returns a single value (usually an aggregate function like MAX or COUNT). If the query does not return a single long value then it will throw a SQLException.



来源:https://stackoverflow.com/questions/19044910/sql-max-min-in-ormlite-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!