在将检索到的数据放入 vector 之前,我有一个结构来存储它们。
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 放入 vector 中。这是我的功能的一部分:
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);
}
最佳答案
尝试一下:
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();
关于c++ - 如何用 C++ 从 MySQL 获取 Blob 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21157244/