-3

I have local SQLite DB with some values. And I have "name" and "description" columns with "string" type.

Values like 32145152174524132151421 can be inserted into these columns.

When I ty to get values using the System.Data.SQLite library, in cases where I use reader.GetValue().toString() or reader.GetString(), I get values like 32321541e+35 or a InvalidCast exception, but the DB stores normal values.

SQLite and its problems with CastValues are just killing me. Can someone help me?

Columns "Zakupki_name" and "Zakupki_description" have a string data type.

I wrote it without a dot at the end, but in DB it saves with dot.

"Zakupki_name"

  • Saved in DB - 12323141276876900118036480.
  • Returned by Code - 1.23231412768769e+25

"Zakupki_description"

  • Saved in DB - 123444441521613002768384.
  • Returned by Code - 1.23444441521613e+23

Code for getting data from the database.

 SQLiteCommand command = new SQLiteCommand(
    @"SELECT Zakupki_id,
    Zakupki_name,
    Zakupki_description,
    Zakupki_update_date,
    Zakupki_value_limit
    FROM Zakupki;"
,connection);

SQLiteDataReader reader = command.ExecuteReader();
int rowCount = GetRowCount(Tables.Zakupki);
if (rowCount != 0)
{
    zakupkis = new List<Zakupki>();
    while (reader.Read())
    {
        zakupkis.Add(new Zakupki(reader.GetInt32(0), reader.GetValue(1).ToString(), reader.GetValue(2).ToString(), DateTime.Parse(reader.GetString(3)), double.Parse(reader.GetValue(4).ToString())));
    }
}

Code for insert values to db.

public void InsertZakupku(string name, string description,double value_limit)
{
    //Добавить закупку
    try
    {
        createConnection();
        SQLiteCommand command = new SQLiteCommand(
            string.Format(@"INSERT INTO Zakupki (
            Zakupki_name,
            Zakupki_description,
            Zakupki_update_date,
            Zakupki_value_limit
            ) VALUES ('{0}','{1}','{2}','{3}');"
        , name, description, DateTime.Now, value_limit), connection); 
                                                   
        command.ExecuteNonQuery();
        closeConnection();
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
        closeConnection();
    }
}

Using method to insert value.

sQLiteClient.InsertZakupku(zakupkiName.Text, zakupkiDescription.Text, double.Parse(zakupkiValue.Text));
Jayme
  • 1,776
  • 10
  • 28
Bors1k
  • 1
  • 2
  • 3
    Please read https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ and edit your question to provide *far* more details, including the schema and the code you're using to write to and read from the database. I strongly suspect that the problem is a misunderstanding of floating point numbers rather than a bug in SQLite. – Jon Skeet Oct 18 '21 at 09:38
  • add code and more explanations – Bors1k Oct 18 '21 at 10:19
  • Does this answer your question? [Double to string conversion without scientific notation](https://stackoverflow.com/questions/1546113/double-to-string-conversion-without-scientific-notation) – Charlieface Oct 18 '21 at 10:24
  • Did you try printing out your insertion SQL? Basically the problem is how you're formatting the `double` value. You have problems of: 1) SQL injection vulnerability - don't use string formatting to create SQL; use parameterized SQL instead. 2) Incorrect expectations of the default string format for `double`. 3) Incorrect expectations of the precision of `double` - your sample value has 27 digits, which is *way* more than `double` can handle. Perhaps you should use `BigInteger`? – Jon Skeet Oct 18 '21 at 10:44
  • My sample value isn't `double`, it is string, i want `string` value only. But it returns numeric format. – Bors1k Oct 18 '21 at 11:23

1 Answers1

0

Fixed this problem by using varchar(255) instead string or text

Bors1k
  • 1
  • 2