简体   繁体   English

c# sqlite - 从数据库中检索 BLOB 文本

[英]c# sqlite - Retrieve BLOB text from database

I have a sqlite database with a single table (currently) and 4 columns:我有一个带有单个表(当前)和 4 列的 sqlite 数据库:

Name //TEXT
Quote //TEXT
ImageURL //TEXT, an online image
Entry //Text BLOB, I want to parse and store this as a List<Entry>

I've made some methods to create a text file, parse and store data and delete the file.我已经使用了一些方法来创建文本文件、解析和存储数据以及删除文件。 The data is stored in this format:数据以这种格式存储:

$"{Index} {Time} {Date} {URL}" // "1 00:00.0 9/13/1985 www.stackoverflow.com"

...so that way, it's easier to split this string and store it in my container class, Entry . ...这样,拆分此字符串并将其存储在我的容器类Entry更容易。 The size of these files averages 250 bytes so database memory will most likely never be an issue.这些文件的大小平均为 250 字节,因此数据库内存很可能永远不会成为问题。

Here are the classes that wire to the column data in my table:以下是连接到我表中列数据的类:

public abstract class BaseClass
{
public abstract string Name {get; set;}
public abstract string ImageURL {get; set;}
public List<Entry> Records {get; private set;} //I already know .Add() is exposed, this problem has been fixed however.
...
}

public class DerivedClass : BaseClass
{
public override string Name {get; set;}
public override string ImageURL {get; set;}
public string Quote {get; set;}
...
}

The solutions I've seen in the past couple days involve different languages and/or BLOB types and none of them use Dapper, Sqlite and C# together.我在过去几天看到的解决方案涉及不同的语言和/或 BLOB 类型,并且没有一个同时使用 Dapper、Sqlite 和 C#。

What I have so far returns a List<DerivedClass> containing a single element that wires to the properties in my derived class I've shown above.到目前为止,我所返回的List<DerivedClass>包含一个元素,该元素连接到我上面显示的派生类中的属性。 Everything but the BLOB, that is.除了 BLOB 之外的所有东西,就是这样。

public static ClassType GetSingleRow(string primaryKey) where ClassType : BaseClass
        { 
            using (IDbConnection connection = new SQLiteConnection(LoadConnectionString()))
            {                
                string sql = $"SELECT * FROM {typeof(ClassType).Name} WHERE Name = \"{primaryKey}\""; //probably vulnerable to sql injection but I'll worry about it later
                var output = connection.Query<ClassType>(sql, new DynamicParameters());
                //how would get a BLOB from this ienumearable?
                return output.ToList()[0]
            }
        }

To reiterate, How would I be able to retrieve BLOB text from my sqlite database with Dapper?重申一下,如何使用 Dapper 从我的 sqlite 数据库中检索 BLOB 文本? Would I need to retrieve it using a byte array?我需要使用字节数组来检索它吗? If so, how would I do that?如果是这样,我该怎么做?

Okay so right before I was going to post this question, I thought, "Why don't I just make another property called "Entry" so Dapper can do the rest of the work for me. I made it a string initially but then I changed it to byte[] after getting the InvalidCastException好吧,就在我要发布这个问题之前,我想,“为什么我不创建另一个名为“Entry”的属性,以便 Dapper 可以为我完成其余的工作。我最初将它设为一个string但后来我获取InvalidCastException后将其更改为byte[]

public class DerivedClass : BaseClass
{
public override string Name {get; set;}
public override string ImageURL {get; set;}
public string Quote {get; set;}
public byte[] Entry {get; set;}
...
}

then in Main() , I can just do然后在Main() ,我可以做

Console.WriteLine(System.Text.Encoding.Default.GetString(DerivedClassObject.Entry));

With my methods to parse the text file, I can easily store this in my List<Entry> class.使用我解析文本文件的方法,我可以轻松地将其存储在我的List<Entry>类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM