<bdo id='BFFJq'></bdo><ul id='BFFJq'></ul>
  1. <small id='BFFJq'></small><noframes id='BFFJq'>

    1. <i id='BFFJq'><tr id='BFFJq'><dt id='BFFJq'><q id='BFFJq'><span id='BFFJq'><b id='BFFJq'><form id='BFFJq'><ins id='BFFJq'></ins><ul id='BFFJq'></ul><sub id='BFFJq'></sub></form><legend id='BFFJq'></legend><bdo id='BFFJq'><pre id='BFFJq'><center id='BFFJq'></center></pre></bdo></b><th id='BFFJq'></th></span></q></dt></tr></i><div id='BFFJq'><tfoot id='BFFJq'></tfoot><dl id='BFFJq'><fieldset id='BFFJq'></fieldset></dl></div>
      <legend id='BFFJq'><style id='BFFJq'><dir id='BFFJq'><q id='BFFJq'></q></dir></style></legend>
      <tfoot id='BFFJq'></tfoot>

      如何从 sqlite 存储和检索 blob?

      时间:2023-10-10

      <small id='GAHUc'></small><noframes id='GAHUc'>

          <tbody id='GAHUc'></tbody>

          <bdo id='GAHUc'></bdo><ul id='GAHUc'></ul>
        • <i id='GAHUc'><tr id='GAHUc'><dt id='GAHUc'><q id='GAHUc'><span id='GAHUc'><b id='GAHUc'><form id='GAHUc'><ins id='GAHUc'></ins><ul id='GAHUc'></ul><sub id='GAHUc'></sub></form><legend id='GAHUc'></legend><bdo id='GAHUc'><pre id='GAHUc'><center id='GAHUc'></center></pre></bdo></b><th id='GAHUc'></th></span></q></dt></tr></i><div id='GAHUc'><tfoot id='GAHUc'></tfoot><dl id='GAHUc'><fieldset id='GAHUc'></fieldset></dl></div>
          <legend id='GAHUc'><style id='GAHUc'><dir id='GAHUc'><q id='GAHUc'></q></dir></style></legend>

              <tfoot id='GAHUc'></tfoot>
              • 本文介绍了如何从 sqlite 存储和检索 blob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在 C++、python 和现在(也许)在 C# 中使用过 sqlite.在所有这些中,我不知道如何将 blob 插入表中.如何在 sqlite 中存储和检索 blob?

                I have used sqlite in c++, python and now (perhaps) in C#. In all of these I have no idea how to insert a blob into a table. How do I store and retrieve a blob in sqlite?

                推荐答案

                以下是您可以在 C# 中执行此操作的方法:

                Here's how you can do it in C#:

                class Program
                {
                    static void Main(string[] args)
                    {
                        if (File.Exists("test.db3"))
                        {
                            File.Delete("test.db3");
                        }
                        using (var connection = new SQLiteConnection("Data Source=test.db3;Version=3"))
                        using (var command = new SQLiteCommand("CREATE TABLE PHOTOS(ID INTEGER PRIMARY KEY AUTOINCREMENT, PHOTO BLOB)", connection))
                        {
                            connection.Open();
                            command.ExecuteNonQuery();
                
                            byte[] photo = new byte[] { 1, 2, 3, 4, 5 };
                
                            command.CommandText = "INSERT INTO PHOTOS (PHOTO) VALUES (@photo)";
                            command.Parameters.Add("@photo", DbType.Binary, 20).Value = photo;
                            command.ExecuteNonQuery();
                
                            command.CommandText = "SELECT PHOTO FROM PHOTOS WHERE ID = 1";
                            using (var reader = command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    byte[] buffer = GetBytes(reader);
                                }
                            }
                
                        }
                    }
                
                    static byte[] GetBytes(SQLiteDataReader reader)
                    {
                        const int CHUNK_SIZE = 2 * 1024;
                        byte[] buffer = new byte[CHUNK_SIZE];
                        long bytesRead;
                        long fieldOffset = 0;
                        using (MemoryStream stream = new MemoryStream())
                        {
                            while ((bytesRead = reader.GetBytes(0, fieldOffset, buffer, 0, buffer.Length)) > 0)
                            {
                                stream.Write(buffer, 0, (int)bytesRead);
                                fieldOffset += bytesRead;
                            }
                            return stream.ToArray();
                        }
                    }
                }
                

                这篇关于如何从 sqlite 存储和检索 blob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:插入过程中出现 SQLite 错误“尝试写入只读数据库"? 下一篇:数据库先创建实体框架6.1.1模型使用system.data.sqlite 1.0.93

                相关文章

                  <bdo id='07QB4'></bdo><ul id='07QB4'></ul>

                <tfoot id='07QB4'></tfoot>

                  1. <small id='07QB4'></small><noframes id='07QB4'>

                  2. <i id='07QB4'><tr id='07QB4'><dt id='07QB4'><q id='07QB4'><span id='07QB4'><b id='07QB4'><form id='07QB4'><ins id='07QB4'></ins><ul id='07QB4'></ul><sub id='07QB4'></sub></form><legend id='07QB4'></legend><bdo id='07QB4'><pre id='07QB4'><center id='07QB4'></center></pre></bdo></b><th id='07QB4'></th></span></q></dt></tr></i><div id='07QB4'><tfoot id='07QB4'></tfoot><dl id='07QB4'><fieldset id='07QB4'></fieldset></dl></div>
                    <legend id='07QB4'><style id='07QB4'><dir id='07QB4'><q id='07QB4'></q></dir></style></legend>