jeudi 22 janvier 2015

Comma vs dot as decimal separator

I have a C# app that writes some data into a SQLite database, the problem comes when I try to store a C# double into a SQLite REAL field.


It saves it into the database with a comma as decimal separator (as usual in my country, not sure it counts for anything).


The table has the following structure:



CREATE TABLE `valoresStock` (
`Fecha` TEXT,
`IdArticulo` TEXT,
`IdColor` INTEGER,
`KgPorUnidad` REAL,
`Stock` REAL,
`CostePorKg` REAL,
`CostePorUnidad` REAL,
PRIMARY KEY(Fecha,IdArticulo,IdColor)
);


And the query I use in my app is the following:



cmd.CommandText = "INSERT INTO valoresStock (Fecha, IdArticulo, IdColor, KgPorUnidad, Stock, CostePorKg, costePorUnidad) VALUES ('" + DateTime.Today + "','" + referencia.idArticulo + "', '" + referencia.idColor + "', '" + referencia.kilos + "', '" + referencia.stockActual + "', '" + referencia.valorPorKg + "', '" + referencia.valorPorUnidad + "') ";


As I said, the four last fields are of type double. No error is given.


Then the data is stored as follows:


data structure


That is, using a dot when it doesn't have decimal part, and a comma when it does.


The problem with this is for example when I use a SELECT statement to calculate, such as this:



SELECT VS.Fecha, VS.IdArticulo, VS.IdColor, (VS.Stock * (VS.CostePorUnidad + VS.CostePorKg * Vs.KgPorUnidad)) AS Valor FROM valoresStock VS


This gives the following result, which is wrong (only took the number with the dot).


error with commas


So If I manually change the commas for dots in my table, it works properly.


all with dots


But how to make my app do this, some kind of culture change?


Or can I make SQLite understand or automatically transform the commas?


Why does SQLite accept that number as valid if it doesn't understand it later?


Aucun commentaire:

Enregistrer un commentaire