【问题标题】:How to insert blob in SQLite in Windows store app developed in c#/XAML如何在用 c#/XAML 开发的 Windows 商店应用程序中的 SQLite 中插入 blob
【发布时间】:2012-10-15 10:47:51
【问题描述】:

我正在开发一个应用程序,其中包含一些媒体文件,如图像/音频/视频剪辑。我想将这些媒体文件作为 blob 插入到我的 C#/XAML Windows 商店应用程序的 SQLite 中。我找不到任何显示 blob 数据类型的示例。我怎样才能做到这一点 ? Here可以看到SQLite支持blob数据类型

【问题讨论】:

    标签: c# windows-8 windows-runtime microsoft-metro windows-store-apps


    【解决方案1】:

    不确定您使用的是哪个 API,但这里有一个针对 SQLite-Net 的单元测试。它所做的只是创建测试对象并用一些数据填充 blob(字节数组)。然后它将保存的对象与原始对象进行比较。

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

    只是为了查看数据库中的内容,我从 SQLite 命令行运行了这个:

    select id, hex(blob) from blobtest;
    

    结果:

    1|0102030405060708090A
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      • 2012-10-14
      • 1970-01-01
      相关资源
      最近更新 更多