-1

In Unity, I want to save mouse sensivity value (float) in database table as REAL. But for some reason it rounds the value, so if I have value 1.0, it saves 1.0, but if it's, for example 0.65, it saves 0.0. Here's the query:

foreach (KeyValuePair<string, float> kv in mouse)
    {
        MyDataBase.ExecuteQueryWithoutAnswer("UPDATE Mouse SET value = '" + kv.Value.ToString() + "' WHERE option = '" + kv.Key + "'");
    }

Other types save correctly.

Nowler
  • 1
  • try to use parameters instead of culture depending string representation – Stefan Aug 02 '20 at 20:29
  • You are converting a numeric value to a string. Then pass this string to the database engine that need to store that string in a numeric field and so it executes another conversion. The end result is an error. Instead if you use parameters of the right datatype and pass a correct datatype as value there is no conversion to execute and no misunderstanding of the decimal separator symbol. – Steve Aug 02 '20 at 20:41

1 Answers1

0

Found out about that comma instead of dot. Made this creepy piece of code and now it works.

foreach (KeyValuePair<string, float> kv in mouse)
    {
        string s = ((int)kv.Value).ToString() + "." + ((kv.Value - (int)kv.Value) * 100).ToString();
        MyDataBase.ExecuteQueryWithoutAnswer("UPDATE Mouse SET value = '" + s + "' WHERE option = '" + kv.Key + "'");
    }
Nowler
  • 1
  • 1
    Yes you have a real wrong solution in your hand. Again look at how to write that query using parameters. I would give an example but I cannot give you it without seeing the code of ExecuteQueryWithoutAnswer – Steve Aug 02 '20 at 20:47
  • 1
    Besides, _other types saved correctly_. No you are not saving correctly them with this method. For example, what happens if you have a string containing the word _O'Brian_ (a quote inside the string). Now let's talk about another big reason to avoid this method. Let's talk about [Sql Injection](https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work) – Steve Aug 02 '20 at 20:51