【问题标题】:Fetching BLOB data from an SQLite table using C++使用 C++ 从 SQLite 表中获取 BLOB 数据
【发布时间】:2021-10-15 14:55:42
【问题描述】:

我有一个 SQLite 数据库,将图像数据存储在一个表中。 该表以整数格式保存图像的高度和宽度,以 BLOB 格式保存实际图像的二进制数据。

我目前正在使用回调函数来获取所需的数据,如下所示,

/*
MethodName      : callback
FUnctionality   : This method is used to fetch the Output data in Collection format based on the Query passed to sqlite3_exec API.
*/
static int callback(void* data, int argc, char** argv, char** azColName)
{
    int i, width, height;
    
    //Iterating columns
    for (i = 0; i < argc; i++) {

        string columnName = azColName[i];

        if (columnName == "Image_Height")
        {
        height= stoll(columnVal);
        }//end of if

        else if (columnName == "Image_Width")
        {
        width = stoll(columnVal);
        }//end of else if

        else if (columnName == "BLOB_DATA")
        {
        **/ *How to fetch BLOB data? */**
        }//end of else if

    }//end of for

    return 0;
}//end of callback

我们如何存储 BLOB 数据,以便在进一步的步骤中转换为实际图像? 我们不知道任何可用于存储 BLOB 数据的方法,因为我们是这个概念的新手。 有什么方法可以满足我们的要求吗?

【问题讨论】:

  • 你最初是如何将BLOB数据存储在表中的?只需颠倒这个过程。
  • 我目前正在使用包含 BLOB 数据的可用本地数据库。目标是从表格中可用的 BLOB 数据中获取数据。
  • 不要使用sqlite3_exec()函数。使用prepared statement 并直接访问BLOB 列数据,方法是将其复制到unsigned char[]

标签: c++ sqlite blob


【解决方案1】:

根据sqlite3_exec documentationargv 参数指向调用sqlite3_column_text 获得的结果数组。 that 函数的 documentation 声明 BLOB 到 TEXT 的转换“在需要时添加零终止符”。

不幸的是,您无法知道该值究竟有多大。 我建议在LENGTH(BLOB_COLUMN) 形式的 SELECT 语句中添加一个额外的输出,它会为您提供 blob 列的大小(以字节为单位)。

然后您可以将列值的前 X 个字节作为图像的内容。

【讨论】:

    猜你喜欢
    • 2013-07-28
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    相关资源
    最近更新 更多