Microsoft.Data.Sqlite error with join and multiple blobs

24 Views Asked by At

my c# project uses nuget package Microsoft.Data.Sqlite I have a query that joins two tables and each of them has a blob field. When I try to read the blob from frist blob from first table I then no more able to read the blob from the second table.

Here the code that reproduces the issue:

using (var connection = new SqliteConnection("Data Source=:memory:")) {
    connection.Open();

    using (SqliteCommand command = connection.CreateCommand()) {
        command.CommandType = CommandType.Text;
        command.CommandText = @"CREATE TABLE A (
                                ID INTEGER PRIMARY KEY,
                                DESCRIPTION TEXT NOT NULL,
                                VALUE BLOB) ";
        command.ExecuteNonQuery();

        command.CommandText = @"CREATE TABLE B (
                                ID INTEGER PRIMARY KEY,
                                FATHER_ID INTEGER NOT NULL,
                                DESCRIPTION TEXT NOT NULL,
                                VALUE BLOB) ";
        command.ExecuteNonQuery();

        command.CommandText = "INSERT INTO A (ID, DESCRIPTION, VALUE) VALUES (1,'Test 1 A', :val)";
        command.Prepare();
        byte[] buff = new byte[] { 0,0,0,0,0,0};
        command.Parameters.Add(new SqliteParameter("val", buff));
        command.ExecuteNonQuery();

        command.CommandText = "INSERT INTO B (ID,FATHER_ID, DESCRIPTION, VALUE) VALUES (10000,1,'Test 1 B', :val)";
        command.Prepare();
        buff = buff = new byte[] { 1, 1, 1, 1, 1, 1 };
        command.Parameters.Clear();
        command.Parameters.Add(new SqliteParameter("val", buff));
        command.ExecuteNonQuery();

        command.CommandText = @"SELECT 
                                    A.ID as AID,
                                    A.DESCRIPTION as ADESC,
                                    A.VALUE as AVALUE,
                                    B.ID as BID,
                                    B.DESCRIPTION as BDESC,
                                    B.VALUE as BVALUE
                                FROM 
                                    A JOIN B
                                    ON B.FATHER_ID=A.ID ";
        var reader = command.ExecuteReader();
        while(reader.Read()) {
            Console.WriteLine($"A.ID={reader.GetInt32(0)} A.DESC={reader.GetString(1)} B.ID={reader.GetInt32(2)} B.DESC={reader.GetString(3)}");
            long abuffLength = reader.GetBytes(2, 0, null, 0, 0); //this line will force to cache rowid=1 from table A
            long bbuffLength = reader.GetBytes(5,0,null,0,0);     //this line will fail because rowid=1 for table B is invalid
        }
    }
}

In a normal case I should be able to read both blobs. Instead this is the stack trace I receive:

Unhandled exception. Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'no such rowid: 1'.
   at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
   at Microsoft.Data.Sqlite.SqliteBlob..ctor(SqliteConnection connection, String databaseName, String tableName, String columnName, Int64 rowid, Boolean readOnly)
   at Microsoft.Data.Sqlite.SqliteDataRecord.GetStream(Int32 ordinal)
   at Microsoft.Data.Sqlite.SqliteDataRecord.GetBytes(Int32 ordinal, Int64 dataOffset, Byte[] buffer, Int32 bufferOffset, Int32 length)
   at Microsoft.Data.Sqlite.SqliteDataReader.GetBytes(Int32 ordinal, Int64 dataOffset, Byte[] buffer, Int32 bufferOffset, Int32 length)
   at Test.Program.Main(String[] args) in 

Any idea what I'm doing wrong or if it is a bug?

0

There are 0 best solutions below