本文介绍了将数据解析为十进制(从sqlite db)时,为什么会出现错误的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 场景 - 加密Sqlite数据库 - System.Data.SQlite.dll(版本1.0.97.9) - .net framework 2.0 - x86 有一个用于将数据写入数据库的线程,以及几个用于读取数据的线程。 表是以下一种方式创建的: Scenario- encrpyted Sqlite database- System.Data.SQlite.dll (version 1.0.97.9)- .net framework 2.0- x86There is one thread for writing data to database, and several threads for reading data.Table is created in next way:CREATE TABLE [TableTransaction] ( [ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, [TOTALIN] DECIMAL(18, 4) DEFAULT 0 ... ); 用于读取数据的代码: Code which is used for reading data:using (DbCommand cmd = SQLiteFactory.Instance.CreateCommand()){ SQLiteConnection cn = (SQLiteConnection)SQLiteFactory.Instance.CreateConnection(); cn.ConnectionString = "Data Source=myDatabase.db; Password=fdsh243ah45"; cn.Open(); cmd.CommandText = "SELECT * FROM TableTransaction"; cmd.CommandType = CommandType.Text; cmd.CommandTimeout = 15; cmd.Connection = cn; using (DbDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { int id = reader.GetInt32("ID"); decimal totalIn = reader.GetDecimal("TOTALIN"); } }} 在数据库中TOTALIN = 0.0001。 如果我从主线程运行这个SELECT命令,那么我得到totalIn = 0.00100000011920928(错误的值)。 但是,如果我从其他一些线程运行它,然后totalIn = 0.0001(这个值没问题)。 我假设有一些从十进制到双精度的内部转换。但是为什么它只发生在主线程上? 什么可能导致这种行为? 我没有玩使用线程,所有线程值都是默认值。In database TOTALIN = 0.0001.If I run this SELECT command from main thread, then I get totalIn = 0.00100000011920928 (wrong value).But, if I run it from some other thread, then totalIn = 0.0001 (this value is ok).I assume that there is some internal casting from decimal to double. But why it happens only on main thread? What could cause this behaviour?I didn't play with threads, all thread values are default values.推荐答案 这两个值几乎相同。显示它们的代码是否相同,即两个线程中显示的位数是否相同? 我看看来自的原始数据数据库为十六进制数字,并确保它是相同的。然后确认查看数据的方法是相同的,并没有稍微不同,最后看一下十进制totalLn的原始十六进制值,看看它们是否相同。 我怀疑某种显示问题(四舍五入,没有显示所有有效数字等等)。Those two values are almost the same. Is the code displaying them identical, that is the same number of digits would be displayed in either thread?I'd have a look at the raw data coming from the database as hex digits and make sure it is the same. Then confirm the method of viewing the data is the same, and not slightly different, and last have a look at the raw hex values of the decimal totalLn to see if they are the same.I'd suspect some sort of display issue (rounding, not showing all significant digits, etc...) 是的,它们几乎是一样的。 显示的代码是相同的,实际上我在visual studio Watch窗口中检查了它。 十六进制表示等于十进制表示 - 值不相同。 我找到了解决方案。我不喜欢它但它的工作原理:P。 我将dblist类型从DECIMAL(18,4)更改为TEXT。 我担心SQLite聚合函数和比较运算符无法与TEXT列一起正常工作,但事实并非如此。他们工作得很好。Yes, they are almost the same.The code for displaying is identical, actually I checked it in visual studio Watch window.Hex representation is equal to decimal representation - values are not same.I found solution. I don't like it but it works :P.I changed db column type from DECIMAL(18, 4) to TEXT. I was afraid that SQLite aggregate functions and comparison operators will not work properly with TEXT column, but that is not the case. They are working just fine. 这篇关于将数据解析为十进制(从sqlite db)时,为什么会出现错误的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 19:24