![](/img/trans.png)
[英]How can I write SQLite Real vals to Java BigDecimal values?
[英]How can I store a BigDecimal (Java) value in a Real (SQlite) column?
我仍然有 BigDecimal 的大问题(眼泪的踪迹从这里开始,一直到这里为止。
现在我遇到了相反的问题 - 从 POJO 中的 BigDecimal 到 SQLite 表中的 REAL。
POJO 定义为:
public class DeliveryItem {
private int _id;
private String _invoiceNumber;
private String _UPC_PLU;
private String _vendorItemId;
private int _packSize;
private String _description;
private BigDecimal _cost;
private BigDecimal _margin;
private BigDecimal _listPrice;
private int _departmentNumber;
private String _subdepartment;
private String _quantity;
public void setID(int id) {
this._id = id;
}
. . .
这段代码试图让数据成形,以便能够发布到 SQLite 表(其中相应的列是数据类型 REAL,SQLite 唯一的实数/浮点数据类型):
public long addDeliveryItem(DeliveryItem delItem) {
long IdAdded = 0;
ContentValues values = new ContentValues();
values.put(COLUMN_INVOICENUM, delItem.get_invoiceNumber());
values.put(COLUMN_UPCPLU, delItem.get_UPC_PLU());
values.put(COLUMN_VENDORITEMID, delItem.get_vendorItemId());
values.put(COLUMN_PACKSIZE, delItem.get_packSize());
values.put(COLUMN_DESCRIPTION, delItem.get_description());
//values.put(COLUMN_COST, delItem.get_cost());
//values.put(COLUMN_COST, new BigDecimal(delItem.get_cost()));
values.put(COLUMN_COST, (Double) delItem.get_cost());
values.put(COLUMN_MARGIN, delItem.get_margin());
values.put(COLUMN_LISTPRICE, delItem.get_listPrice());
values.put(COLUMN_DEPTNUM, delItem.get_departmentNumber());
values.put(COLUMN_SUBDEPT, delItem.get_subdepartment());
values.put(COLUMN_QTY, delItem.get_quantity());
SQLiteDatabase db = this.getWritableDatabase();
if (db != null) {
IdAdded = db.insert(TABLE_DELIVERYITEMS, null, values);
}
if (db != null) {
db.close();
}
return IdAdded;
}
对于上面的 COLUMN_COST 行(“MARGIN”和“LISTPRICE”列有相同的问题),当我尝试这样做时,我得到“错误:不兼容的类型:BigDecimal 无法转换为 Double ”:
values.put(COLUMN_COST, (Double) delItem.get_cost());
...对于注释掉的代码(两行),即:
values.put(COLUMN_COST, delItem.get_cost());
...还有这个:
values.put(COLUMN_COST, new BigDecimal(delItem.get_cost()));
...我得到:
error: no suitable method found for put(String,BigDecimal)
method ContentValues.put(String,String) is not applicable
(argument mismatch; BigDecimal cannot be converted to String)
method ContentValues.put(String,Byte) is not applicable
(argument mismatch; BigDecimal cannot be converted to Byte)
method ContentValues.put(String,Short) is not applicable
(argument mismatch; BigDecimal cannot be converted to Short)
method ContentValues.put(String,Integer) is not applicable
(argument mismatch; BigDecimal cannot be converted to Integer)
method ContentValues.put(String,Long) is not applicable
(argument mismatch; BigDecimal cannot be converted to Long)
method ContentValues.put(String,Float) is not applicable
(argument mismatch; BigDecimal cannot be converted to Float)
method ContentValues.put(String,Double) is not applicable
(argument mismatch; BigDecimal cannot be converted to Double)
method ContentValues.put(String,Boolean) is not applicable
(argument mismatch; BigDecimal cannot be converted to Boolean)
method ContentValues.put(String,byte[]) is not applicable
(argument mismatch; BigDecimal cannot be converted to byte[])
那么如何操作 BigDecimal 值以便 ContentValues 实例接受它呢?
我可能不得不暂时通过忽略这些 val 并将 DDL 更改为:
//+ COLUMN_COST + " REAL," + COLUMN_MARGIN + " REAL," + COLUMN_LISTPRICE + " REAL,"
+ COLUMN_COST + " REAL DEFAULT 0," + COLUMN_MARGIN + " REAL DEFAULT 0," + COLUMN_LISTPRICE + " REAL DEFAULT 0,"
那么如何操作 BigDecimal 值以便 ContentValues 实例接受它呢?
好吧,您可以在BigDecimal
上调用doubleValue()
将其降级为double
,它可以进入ContentValues
(在将其自动装箱为Double
)。 或者,您可以将其字符串表示形式存储在TEXT
列中。 或者,您可以将unscaledValue()
和scale()
在两个INTEGER
列中。
但 SQLite 最接近的匹配是 REAL
不它不是。
您似乎对在 SQLite 中存储定价数据感兴趣。 在主要搜索引擎上搜索storing prices in sqlite
:
共识是将值存储为INTEGER
列,以最小的单个货币单位定价(例如,以美元为单位的货币价值的美分)或其他适当的东西(例如,如果这是最精细的粒度,则为十分之一美分)价格)。
然后,您的 POJO 将保存int
值以进行匹配。
有两种可能:
String
。第一个应该很简单,第二个我会指出一个关于类似主题的很好的问题,如何在 Sqlite 中将图像存储为 blob 以及如何检索它?
我使用 SQLite Integer 类型并像这样转换:
BigDecimal bd = new BigDecimal("1234.5678");
int packedInt = bd.scaleByPowerOfTen(4).intValue(); // packedInt now = 12345678
现在将packedInt 保存到SQLite Integer 字段。
回到 BigDecimal:
BigDecimal bd = new BigDecimal(packedInt); // bd = 12345678
bd = bd.scaleByPowerOfTen(-4); // now bd = 1234.5678
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.