Farhan Ghumra
Farhan Ghumra

Reputation: 15296

How to insert blob in SQLite in Windows store app developed in c#/XAML

I am developing an app which has some media files like images/audio/video clip. I want to insert those media files as blob into SQLite of my C#/XAML windows store app. I can't find any example showing the blob datatype. How can I do that ? Here you can see SQLite supports blob datatype

Upvotes: 3

Views: 1014

Answers (1)

chue x
chue x

Reputation: 18803

Not sure which API you are using, but here is a unit test for SQLite-Net. All it does is create the test object and populates the blob (byte array) with some data. It then compares the saved object with the original object.

public class BlobTest
{
    public int Id { get; set; }
    public byte[] Blob { get; set; }
}

[TestMethod]
public void TestSaveBlob()
{
    var conn = new SQLiteConnection("path_to_db");
    conn.CreateTable<BlobTest>();

    var expected = new BlobTest() { Id = 1 };
    expected.Blob = new byte[10];
    for (int idx = 0; idx < expected.Blob.Length; idx++)
    {
        expected.Blob[idx] = (byte)(idx + 1);
    }

    conn.Insert(expected);

    var actual = conn.Table<BlobTest>().FirstOrDefault();
    Assert.IsTrue(actual.Id != 0, string.Format("actual.Id == '{0}', expected non-zero", actual.Id));
    Assert.IsTrue(actual.Id == expected.Id, string.Format("actual.Id == '{0}', expected '{1}'", actual.Id, expected.Id));
    Assert.IsTrue(actual.Blob != null, string.Format("actual.Blob == '{0}', expected non-null", actual.Blob));

    for (int idx = 0; idx < expected.Blob.Length; idx++)
    {
        Assert.IsTrue(expected.Blob[idx] == actual.Blob[idx], string.Format("actual.Blob[{0}] == '{1}', expected '{2}'", idx, actual.Blob[idx], expected.Blob[idx]));
    }
}

Just to see what was in the db I ran this from the SQLite command line:

select id, hex(blob) from blobtest;

Results:

1|0102030405060708090A

Upvotes: 1

Related Questions