Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Scenario
- encrpyted Sqlite database
- System.Data.SQlite.dll (version 1.0.97.9)
- .net framework 2.0
- x86

There is one thread for writing data to database, and several threads for reading data.
Table is created in next way:

SQL
CREATE TABLE [TableTransaction] (
  [ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  [TOTALIN] DECIMAL(18, 4) DEFAULT 0
  ...
  );


Code which is used for reading data:
C#
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");
        }    
    }
}


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.
Posted
Updated 5-Aug-15 4:07am
v2

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...)
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900