I have various double
values in my database. To get the sum of them all I am using
relvantDao.queryRawValue("SELECT SUM(productPrice * productQuantity) FROM cart");
Problem is that this only returns a long
value, hence the decimal points are lost. How can I get the REAL
sum of these values?
Problem is that this only returns a long value, hence the decimal points are lost. How can I get the REAL sum of these values?
If you look at the ORMLite docs on raw queries, you can see that there is a number of ways to use the raw queries with different field types. To quote:
You can also map the results into your own object by passing in a RawRowMapper object. This will call the mapping object with an array of strings and allow it to convert the strings into an object...
You can also define your own custom mapper if the results are more complex.
This would look something like:
GenericRawResults<Double> rawResults =
orderDao.queryRaw(
"SELECT SUM(productPrice * productQuantity) FROM cart",
new RawRowMapper<Double>() {
public Double mapRow(String[] columnNames, String[] resultColumns){
return Double.parseDouble(resultColumns[0]);
}
});
There are other examples in the ORMLite docs. For example, you can specify a DataType[]
argument and have ORMLite do the extraction from the database natively to a Double
as well.