3
using (SQLiteCommand cmd = ctx.DB.Query("INSERT INTO tableName(Text1, Text2) VALUES (@text1, @text2)"))
{
      cmd.Prepare();
      cmd.Parameters.AddWithValue("@text1","abc");
      cmd.Parameters.AddWithValue("@text2", "123");
      cmd.ExecuteNonQuery();
}

This command leads to an error in the database. If I insert a bunch of data it converts the text2 value to a double instead of just a text with the value "123"

How do I disable the auto convert in the sqllite driver

Table Defintion:

CREATE TABLE `TableName` ( `Text1` TEXT NOT NULL, `Text2` TEXT NOT NULL )

EDIT:

    cmd.Parameters.AddWithValue("@text1", SqlDbType.Text).Value = "abc";
    cmd.Parameters.AddWithValue("@text2", SqlDbType.Text).Value = "123";
    cmd.ExecuteNonQuery();

Results into an exception:

Exception is :

Conversion Exception wrong format
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
   at System.Convert.ToInt32(Object value, IFormatProvider provider)
   at System.Data.SQLite.SQLiteStatement.BindParameter(Int32 index, SQLiteParameter param) in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteStatement.cs:Line 481.
   at System.Data.SQLite.SQLiteStatement.BindParameters() in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteStatement.cs:Line 237.
   at System.Data.SQLite.SQLiteCommand.BuildNextCommand() in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteCommand.cs:Line 388.
   at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index) in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteCommand.cs:Line 395.
   at System.Data.SQLite.SQLiteDataReader.NextResult() in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteDataReader.cs:Line 1864.
   at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave) in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteDataReader.cs:Line 123.
   at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior) in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteCommand.cs:Line 942.
   at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(CommandBehavior behavior) in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteCommand.cs:Line 990.
   at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery() in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteCommand.cs:Line 975.
6
  • Show the table definition. Commented Apr 11, 2018 at 16:20
  • 1
    I don't know if this is a problem with SQLite, but in SQL Server AddWithValue has a bad habit of changing numeric strings to INTs. You should instead use cmd.Parameters.Add("@Parameter", SqlDbType.Foo, ...).Value = .... Commented Apr 11, 2018 at 16:24
  • @DourHighArch already tested. Leads to an exception Commented Apr 11, 2018 at 16:40
  • writing it with native sql insert code leads to valid inserts Commented Apr 11, 2018 at 16:44
  • 1
    You're still using .AddWithValue, use .Add. From the stack trace it looks like SQLite is doing the text -> INT thing as well. Commented Apr 11, 2018 at 16:44

1 Answer 1

4
using (SQLiteCommand cmd = ctx.DB.Query("INSERT INTO tableName(Text1, Text2) VALUES (@text1, @text2)"))
    {
          cmd.Prepare();
          cmd.Parameters.Add("@text1", DbType.AnsiString).Value = "abc";
          cmd.Parameters.Add("@text2", DbType.AnsiString).Value = "123";
          cmd.ExecuteNonQuery();
    }

Thanks to @DourHighArch for that hint

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.