darck
darck
CC#
Created by darck on 12/16/2023 in #help
Crazy database results I can't manage to debug.
Hi, I'm force by a college professor, for an assignement to do integration testing of a mysql database using sqlite. So I'm using the IDbConnection, IDbCommand and IDataReader interfaces from ADO.NET to make it work with whatever. After dealing with datetime and timestamp issues, due to the differences of the sql dialects, I'm running into into a verry annoying issue where SQLite doesn't let you specify the length of integers you want (no need to do so according to it's documentation as it adapst by itself depending based on the length of the value you insert).
int id = (int)(long)reader["id"]; //reader is an IDataReader that has the results from a querry ran with IDbCommand.ExecuteReader()

System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Int64'.
at Stas.Monitor.Infrastructures.ThermometresRepository.GetThermometres() in /home/mathieub/Documents/b2/ai/Stas.Monitor/Stas.Monitor.Infrastructures/ThermometresRepository.cs:line 27
at Stas.Monitor.App.App.OnFrameworkInitializationCompleted() in /home/mathieub/Documents/b2/ai/Stas.Monitor/Stas.Monitor.App/App.axaml.cs:line 58
int id = (int)(long)reader["id"]; //reader is an IDataReader that has the results from a querry ran with IDbCommand.ExecuteReader()

System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Int64'.
at Stas.Monitor.Infrastructures.ThermometresRepository.GetThermometres() in /home/mathieub/Documents/b2/ai/Stas.Monitor/Stas.Monitor.Infrastructures/ThermometresRepository.cs:line 27
at Stas.Monitor.App.App.OnFrameworkInitializationCompleted() in /home/mathieub/Documents/b2/ai/Stas.Monitor/Stas.Monitor.App/App.axaml.cs:line 58
the problem is that if I don't do this then sometimes sqlite decides it's gonna be a System.Int64 and I therefore need to cast it into a long before casting it into an int. I don't know why sqlite does this as every value I ever insert into the database is under the 2 billion something max value for a signed int on 32bits. Is there a way, without knowing the type before runtime to just makes whatever type of int its going to be into an System.Int32 regardless of the number of bits it was originally stored on. (reader can be acessed as a dictionnary of objects) ?
8 replies