Dean
Dean

Reputation: 517

c++ sqllite3 reading blob won't work

Can someone guide me please,

I can't seem to read my blob correctly.

I don't know what's wrong, can somebody help?

this is my function:

what i'm trying to do is:

read the bob as binary and store the bytes in a char *data;

can someone please help?

    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: %s\n", 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;
}

Upvotes: 1

Views: 1584

Answers (1)

Ivan
Ivan

Reputation: 2057

It seems you don't understand what "pointer" really is and how to use it.

Then, sqlite3_column_bytes returns int not int*:

int length = sqlite3_column_bytes(res, 2);

This is absolutely incorrect in current case:

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

If you're using C++ - try to not explicitly use malloc/new, use smart pointer or STL containers instead:

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

This is it.

Upvotes: 3

Related Questions