Home > Enterprise >  SQLite return wrong string value C#
SQLite return wrong string value C#

Time:10-26

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));

CodePudding user response:

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

  • Related