DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: hebhanax
今日帖子: 46
在线用户: 18
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 sxqwhxq (步惊云) ★☆☆☆☆ -
普通会员
2022/7/4 17:33:46
标题:
如何防止sqlite将小数当整数处理? 浏览:887
加入我的收藏
楼主: 一个用delphi写的linux数据库系统,数据字段定义为货币(demcial),但实际操作时,经常保存的记录的全是整数,此时sqlite会把所有数据当成整数运算,导致除法运算丢失精度 ,但如果只要有一条记录数据是浮点数,则能保证运行精度。应当怎样处理这个问题呢?
----------------------------------------------
-
作者:
男 dbyoung (dbyoung) ★☆☆☆☆ -
普通会员
2022/7/4 18:36:34
1楼: 我历来都喜欢用字符串类型,就没有这么多烦心事了。JSON 也都是字符串。

当然我只是偶尔,不是专业弄数据库的。
----------------------------------------------
武汉天气不好
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/7/4 21:35:35
2楼: Here a excelent explaining about SQLite and "Providers conversors"!

https://stackoverflow.com/questions/44298684/sqlite-not-storing-decimals-correctly

-----
Using FireDAC you can "MAP" the fields to do this convertion automatically - transparent to end-user!

any other provider like UniDAC, it's similar!

basically, you define the field or fields, or types these fields to be converted in another type, like:

... myfieldx - type integer - type float
or
... myfield*  - type integer - type float
or
... *      - type integer* - type float
or
... myFieldFloat - type float - type string

some like:

"if" field named "myfieldx", convert integer to float
"if" all fields started by "myfield" convert integer to float
"if"  all fields type "integer" convert to "float"
"if" my field "myFieldFloat" convert float to string 

you understood?

if FireDAC it's done in "2 clicks" on FDManager, or FDConnection, or FDQuery/FDTable, etc...  

--- all start in FDManager and it's inherited to sub-components...


NOTE: of course, you need test if the end value it's correct after convertion!!!
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 hardnut (麦轲数据管家) ★☆☆☆☆ -
普通会员
2022/7/5 11:01:31
3楼: 如果你在sqlite中定义字段类型为demcial, 则它会被认为是 numeric affinity.是一个非常不确定的类型,我觉得你应该将它定义为real.

Numeric --A column with a numeric affinity will store any of the five types. Values with integer
and float types, along with NULL and BLOB types, are stored without conversion.
Any time a value with a text type is stored, an attempt is made to convert the value
to a numeric type (integer or float). Assuming the conversion works, the value is
stored in an appropriate numeric type. If the conversion fails, the text value is stored
without any type of conversion.
----------------------------------------------
UniKeeper V10.40 -- 您最贴心的个人数据管理助手
作者:
男 pp0123 (pp0123) ★☆☆☆☆ -
普通会员
2022/7/5 12:52:38
4楼: Sqlite 的浮点数不是真的浮点数, 即不是转回二进制模式储存. 你可假设它储存的像是类似字串的模式. 因此楼上说得对, Sqlite 的一切都以字串方式储存吧, 避免一堆虫害.
----------------------------------------------
-
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行82.03125毫秒 RSS