Strings which contain NUL '\0' are truncated on read
I'm using EF Core with both SQL Server and SQLite.
I can insert a string which contains a NUL character "foo\0bar". It seems to be written correctly.
I can read it back using a regular sql connection. But if I use EF to read it back, I get only "foo".
This is by design for Microsoft.Data.Sqlite. We use NUL to determine the end of the string during read.
The SQLite documentation also states:
The result of expressions involving strings with embedded NULs is undefined.
Note, in 2.0 Microsoft.Data.Sqlite is building on top of SQLitePCL.raw which does the same thing.
OK by design then. Thanks for clarification.
@denisz1 @T18970237136 It looks like this is "fixed" in version 3.0.0
Hi @bricelam,
in which area was this fixed? When I use Microsoft.Data.Sqlite 3.1.4 and test with a raw INSERT and SELECT using an SqliteCommand, strings still seem to be cut off after a NUL (\0):
using var memoryDb = new SqliteConnection(new SqliteConnectionStringBuilder {
DataSource = ":memory:"
}.ToString());
memoryDb.Open();
const string checkValue = "a\0b\0c";
using var cmd = memoryDb.CreateCommand();
cmd.CommandText = "CREATE TABLE T1(a TEXT); INSERT INTO T1 (a) VALUES (@P1);";
cmd.Parameters.AddWithValue("@P1", checkValue);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cmd.CommandText = "SELECT a FROM T1;";
string strValue = (string)cmd.ExecuteScalar();
Console.WriteLine("String is equal: " + (strValue == checkValue)); // prints "False"
@T18970237136 Can you submit a new issue? Comments on closed issues tend to get lost or forgotten.