0

I want to save a double into my SQLite DB with C# and read it out again.

Now the problem:

At first the input:

If i save a double with a point ( e.g 20.50 ) an insert it in my database i get 2050.0.

If i save it with a comma ( e.g 20,30 ) i get in my db 20,3. That what i want.

Code Input:

String sql = $"INSERT INTO tablename (number) VALUES ('{number}')";                         

SQLiteConnection dbConnection = new SQLiteConnection("Data Source = " + nameDB + ".sqlite; Version = 3;");
dbConnection.Open();
SQLiteCommand Command = new SQLiteCommand(sql, dbConnection);
Command.ExecuteNonQuery();
Command.Parameters.Clear();
dbConnection.Close();

number is a double in c# and in sqllite REAL

But now the output:

If in my db is the value 20,3 and i read it out with:

double number = reader.GetDouble(reader.GetOrdinal("tablename"));

Error: InvalidCastException: Specified cast is not valid.

or

decimal number = reader.GetDecimal(reader.GetOrdinal("tablename"));

Error: wrong inputformat

If i use the reader without the get-methods i get the number without decimals after the comma like int.

Both works if the format in the db is with a point instead of a comma. But this doesnt work for the input.

Is there a solution for this problem?

  • It would be awesome if you could provide an [mcve] of how you are inserting into the database. Please also share the data types (in the DB) of each of the columns. – mjwills Jul 04 '18 at 12:37
  • 1
    Why are you storing a number in a string in the database? – mjwills Jul 04 '18 at 12:42
  • @mjwills worked first time with sqllite db and found this solution to store data –  Jul 04 '18 at 12:44
  • If you are storing the number in a string in the database (which is extremely bad practice) - then this appears to be a localisation issue. Try reading the value as a string & parsing the string to double or decimal specifying the decimal separator as comma - see bottom answers here : https://stackoverflow.com/questions/19893407/string-to-decimal-conversion-dot-separation-instead-of-comma – PaulF Jul 04 '18 at 13:41

0 Answers0