Home > Net >  How to getBlob data from sql and display in gridview using pixmap in C 98?
How to getBlob data from sql and display in gridview using pixmap in C 98?

Time:09-27

I am new in C .I am trying to load blob image which is stored in .bin file from sql Database. That .bin file store blob image in form of string like BM6ect(each .bin have 3 to 4 char).

Here is code that save the blob image(.bmp) in db

        QPixmap p;
        p.load(filename);

  
        string a = "/root/QtApplication_1/file";        
        string b = boost::lexical_cast<std::string>(counter);
        string c = ".bmp";
        
        p.save(("/root/QtApplication_1/file" boost::lexical_cast<std::string>(counter) ".bmp").c_str(),"BMP");
        std::string d = a   b   c ;
        
        
        std::ifstream blob_file;
        blob_file.open(d.c_str(), std::ios_base::binary | std::ifstream::in);
        
    driver = get_driver_instance();
    con = driver->connect("localhost","","");
    con->setSchema("BitmapImagesSchema");

    
    prep_stmt = con->prepareStatement("Insert into bitmapImagesTable(`ID`,`ImageDir`,`ImagesBitMap`) values(?,?,?)");
    prep_stmt->setInt(1,counter);
 // 
    prep_stmt->setString(2,d.c_str());
  //  byte *p = "" ;
    prep_stmt->setBlob(3,&blob_file);
        
    prep_stmt->executeQuery();
   
    delete prep_stmt;

Here is my code(not working) that i am trying to get blob image from db and dispaly in QgraphicsView using pixmap

driver = get_driver_instance();
con = driver->connect("localhost","","");
con->setSchema("BitmapImagesSchema");
stmt = con-> createStatement();

std::istream *blobData;
res = stmt->executeQuery("select `ImagesBitMap` from bitmapImagesTable where `ID`='" boost::lexical_cast<std::string>(counter) "'order by `ID` DESC");
while(res->last()){
    blobData = res->getBlob("ImagesBitMap");
    break;
}

std::istreambuf_iterator<char> isb = std::istreambuf_iterator<char>(*blobData);
std::string blobString  = std::string(isb,std::istreambuf_iterator<char>());
const char * image = blobString.c_str();
blobData->seekg(0,ios::end);
size_t imagesize = blobData->tellg();

cout<<"aaa="<<image<<"\n";
QPixmap p;
p.load(image);
if(!widget.graphicsView_2->scene()){
QGraphicsScene  *scene = new QGraphicsScene(this);
widget.graphicsView_2->setScene(scene);
}
        
widget.graphicsView_2->scene()->addPixmap(p);



delete res;
delete stmt;
delete con;

Graphics view that is using pixmap is not display blob image on button click that execute above code. The code for reading blob image i have taken from this enter image description here

Here is the out put of the image varibale

cout<<"aaa="<<image<<"\n";

enter image description here

How to display blob image by using above code?

CodePudding user response:

Try this code

    driver = get_driver_instance();
    con = driver->connect("localhost","","");
    con->setSchema("BitmapImagesSchema");
    stmt = con-> createStatement();   
    std::istream *blobData;
    res = stmt->executeQuery("select `ImagesBitMap` from bitmapImagesTable where `ID`='" boost::lexical_cast<std::string>(counter) "'order by `ID` DESC");
    while(res->last()){
        blobData = res->getBlob("ImagesBitMap");
        break;
    }
    
    std::istreambuf_iterator<char> isb = std::istreambuf_iterator<char>(*blobData);
    std::string blobString  = std::string(isb,std::istreambuf_iterator<char>());
    const char * image = blobString.c_str();
    blobData->seekg(0,ios::end);
    size_t imagesize = blobData->tellg();
    
    
    cout<<"aaa="<<image<<"size_t="<<imagesize<<"\n";
    QPixmap p;
    p.loadFromData(( const uchar*)image,imagesize);
    if(!widget.graphicsView_2->scene()){
        QGraphicsScene  *scene = new QGraphicsScene(this);
        widget.graphicsView_2->setScene(scene);
    }
            
    widget.graphicsView_2->scene()->addPixmap(p);
    
   
   
    delete res;
    delete stmt;
    delete con;
  • Related