1

I've created table with column type INT8. When I try to execute select query using SQLiteCommand.ExecuteScalar(C#) it returns SByte instead of long.

Why?

kayess
  • 3,384
  • 9
  • 28
  • 45
  • 1
    Please re-read what you have typed... Actually int8 is a byte, when long is 64 bit... – kayess Aug 17 '17 at 10:48
  • According to sqlite docs INT8 is long. Digit 8 means bytes, not bits. Maybe answer is here: [link](https://stackoverflow.com/questions/7337882/what-is-the-difference-between-sqlite-integer-data-types-like-int-integer-bigi) – Dmitry Ivanov Aug 17 '17 at 11:42
  • Well nope. C# wise check [INT8](https://stackoverflow.com/a/24947147) here also check long on [MSDN](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/integral-types-table)... – kayess Aug 17 '17 at 12:22
  • You are absolutly right that in C# it's type System.SByte. But i want to know, if I execute query "ALTER TABLE a ADD COLUMN b INT8;", which column will be created. [SQLiteApi](http://www.sqlite.org/datatype3.html) does not give clear answer, is it 8 bytes or 8 bits. I have made little experiment. I inserted value '129' and tried to read it in different ways. The main magic is that SQLiteCommand.ExecuteScalar reads this value as '-127'(System.SByte) and SQLiteDataReader.GetLong reads the same field as '129'(System.Int64). – Dmitry Ivanov Aug 17 '17 at 13:37
  • **Update**. Line from System.Data.SQLite.dll source: `new SQLiteDbTypeMapping("INT8", DbType.SByte, false)`. – Dmitry Ivanov Aug 17 '17 at 16:31
  • Please add a self answer if it solves the question. – kayess Aug 17 '17 at 17:31

1 Answers1

3

Part of data type mapping in System.Data.SQLite.dll source:

new SQLiteDbTypeMapping("INT8", DbType.SByte, false),
new SQLiteDbTypeMapping("INT16", DbType.Int16, false),
new SQLiteDbTypeMapping("INT32", DbType.Int32, false),
new SQLiteDbTypeMapping("INT64", DbType.Int64, false),
new SQLiteDbTypeMapping("INTEGER", DbType.Int64, true)

According to source code INT8 is System.SByte. In fact .Net API for SQLite allows to store INT64 values into INT8 field. This value can be read in two ways:

  1. SQLiteDataReader.GetInt64 reads value directly without losing data.
  2. SQLiteCommand.ExecuteScalar intitialy gets field type, then reads value with specified cast. This operation can loose data.

In my test I've stored value '129' into INT8 field. Then I tried to read this value.

  • SQLiteDataReader.GetInt64 returned '129' (System.Int64).
  • SQLiteCommand.ExecuteScalar retured '-127' (System.SByte).