SQLite 的浮点类型和顺序问题
我正在使用一个设置了 SQLite 数据库的 iPhone 应用程序。我在数据库中有一个 FLOAT 类型的字段,所以我有像 10,500.00 这样的值。 这里的问题是,如果我选择该表并按该字段排序,则值会像字符串值一样排序。
这是我得到的一个例子:
SELECT floatField FROM table ORDER BY floatField ASC;
结果是:
109,800.00
48,950.00
53,600.00
54,790.00
74,305.00
这显然是错误的,因为 109,800.00 > 48,950.00 作为浮点值。 所以我认为 SQLite 正在像处理字符串一样处理这些值,其中 109,800.00 < 48,950 其中字符串 '1' <字符串“4”。
如果我使用此查询将值转换为浮点数:
SELECT cast(floatValue as Float) FROM table ORDER BY floatField ASC;
我得到的结果是这样的:
109
48
53
54
74
所以这也是错误的。
任何想法都会受到欢迎!
I´m working with an iPhone app which has a SQLite database set up. I have a field in the database which is FLOAT type, so I have values like 10,500.00.
The thing here is that if I select that table and order by that field, values are ordered as if it was a string value.
Here is an example of what I´m getting:
SELECT floatField FROM table ORDER BY floatField ASC;
And the result is:
109,800.00
48,950.00
53,600.00
54,790.00
74,305.00
This is obviously wrong, cause 109,800.00 > 48,950.00 as float values.
So I think SQLite is working with this values as if they were strings, where 109,800.00 < 48,950 where string '1' < string '4'.
If I cast the value to float using this query:
SELECT cast(floatValue as Float) FROM table ORDER BY floatField ASC;
The result I get is this:
109
48
53
54
74
So this is also wrong.
Any ideas will be welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议插入数据时不要使用逗号。 SQLite 对数据进行了一些转换,因此
一切都不同。如果可能,请使用第一个版本,只有这样才能确保正确解析该值。
我自己无法重现这个问题,但也许是因为我不使用相同版本的 SQLite。
I suggest not to use the comma when inserting the data. SQLite transforms the data a bit, so that
are all different. Use the first version if possible, only that way you are sure the value is parsed correctly.
Myself, I can't reproduce the problem, but maybe because I don't use the same version of SQLite.