C++ SQLite3 读取 blob 不起作用



有人可以指导我吗,

我似乎无法正确读取我的斑点。

我不知道怎么了,有人可以帮忙吗?

这是我的函数:

我想做的是:

将 Bob 读取为二进制文件,并将字节存储在字符 *data 中;

有人可以帮忙吗?

    int baramdb::dbreadblob(int pid)
{
    sqlite3_stmt *res;
    const char *tail;
    int count = 0;
    this->dbopen(this->dbfile); 
    if (sqlite3_prepare_v2(this->db, "SELECT * FROM Packet_Send_Queue", 128, &res, &tail) != SQLITE_OK)
    {
        printf("[Baram] Can't retrieve data: %sn", sqlite3_errmsg(db));
        sqlite3_close(db);
        return(1);
    }
    while (sqlite3_step(res) == SQLITE_ROW)
    {
        int *plength = 0;       
        *plength = sqlite3_column_bytes(res, 2);
        unsigned char **pbuffer = (unsigned char **)malloc(*plength);
        memcpy(*pbuffer, sqlite3_column_blob(res, 0), *plength);
        count++;
    }
    sqlite3_close(this->db);        
    this->lastresult = count;
    return count;
}

您似乎不了解"指针"到底是什么以及如何使用它。

然后,sqlite3_column_bytes返回intint*

int length = sqlite3_column_bytes(res, 2);

这在当前情况下是绝对不正确的:

unsigned char **pbuffer = (unsigned char **)malloc(*plength);

如果您使用的是C++ - 尽量不要显式使用 malloc/new ,请改用智能指针或 STL 容器:

std::vector<char> data( length );
const char *pBuffer = reinterpret_cast<const char*>( sqlite3_column_blob(res, 2) );
std::copy( pBuffer, pBuffer + data.size(), &data[0] );

就是这样。

最新更新