使用 ORMLite 从 SUM 中获取 REAL 的方法是什么
What is the method to get a REAL from SUM using ORMLite
我的数据库中有各种 double
值。为了得到它们的总和,我正在使用
relvantDao.queryRawValue("SELECT SUM(productPrice * productQuantity) FROM cart");
问题是这只有 returns 一个 long
值,因此小数点丢失了。我怎样才能得到这些值的 REAL
总和?
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?
如果您查看 ORMLite docs on raw queries,您会发现有多种方法可以使用具有不同字段类型的原始查询。引用:
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.
这看起来像:
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]);
}
});
ORMLite 文档中还有其他示例。例如,您可以指定一个 DataType[]
参数,并让 ORMLite 从数据库本地提取到 Double
。
我的数据库中有各种 double
值。为了得到它们的总和,我正在使用
relvantDao.queryRawValue("SELECT SUM(productPrice * productQuantity) FROM cart");
问题是这只有 returns 一个 long
值,因此小数点丢失了。我怎样才能得到这些值的 REAL
总和?
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?
如果您查看 ORMLite docs on raw queries,您会发现有多种方法可以使用具有不同字段类型的原始查询。引用:
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.
这看起来像:
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]);
}
});
ORMLite 文档中还有其他示例。例如,您可以指定一个 DataType[]
参数,并让 ORMLite 从数据库本地提取到 Double
。