如何在C ++中从MySQL获取Blob数据

时间:2014-01-16 08:57:22

标签: c++ mysql get blob

我有一个结构来存储检索到的数据,然后再将它们放入矢量中。

typedef struct{
    long long int uuId;
    short int gender;
    short int age;
    short int cameraNo;
    unsigned char *image; //[IMAGE_SIZE];
    size_t imageSize;
    //std::string time;
}FaceRecord;

如何获取blob数据并将其放入char *或char []? tempFR是push_back进入向量的临时结构。这是我职能的一部分:

std::stringstream s;
        s << "SELECT * FROM Dao WHERE gender = "<< data <<"";

        prepStmt = con->prepareStatement (s.str());
        res = prepStmt->executeQuery();

        unsigned char* ptr;
        size_t blobSize=100;
        std::istream *is;
        while(res->next()){
            tempFR.uuId = res->getInt64("uuId");
            tempFR.cameraNo = res->getInt("cameraNo");
            tempFR.age = res->getInt("age");
            tempFR.gender = res->getInt("gender");
            is = res->getBlob("image");
            is->seekg (0, std::ios::end);
            blobSize = is->tellg();
            is->seekg (0, std::ios::beg);
            tempFR.image = new unsigned char[blobSize];
            is->read((char*)tempFR.image,blobSize);
            tempFR.imageSize = blobSize;
            rec->push_back(tempFR);
        }

1 个答案:

答案 0 :(得分:2)

尝试一下:

std::istream *blobData = set->getBlob("image");
std::istreambuf_iterator<char> isb = std::istreambuf_iterator<char>(*blobData);
std::string blobString = std::string(isb, std::istreambuf_iterator<char>());
tempFR.image = blobString.c_str();
blobData->seekg(0, ios::end);
tempFR.imageSize = blobData->tellg();