List:MySQL++« Previous MessageNext Message »
From:Valeri Tishev Date:January 7 2009 5:25pm
Subject:saving BLOB to a file
View as plain text  
I am trying to extract a blob from a database. Extraction of other 
columns in table works just fine, but when I try to use the blob it 
occurs to be empty... Could anyone tell me what I am doing wrong? Below 
is the listing of my program. Thanks in advance.

#include <iostream>
#include <fstream>
#include <mysql++.h>
#include <ssqls.h>

#define DATABASE "db"
#define HOST "localhost"
#define USER "root"
#define PASSWORD ""

sql_create_4(record, 1, 4,
    mysqlpp::sql_varchar, id,
    mysqlpp::sql_varchar, name,
    mysqlpp::sql_blob, blob,
    mysqlpp::sql_timestamp, timestamp)

using namespace std;

int main(void) {

     try {
        mysqlpp::Connection connection(false);
        connection.connect(DATABASE, HOST, USER, PASSWORD);
        vector<record> results;
        mysqlpp::Query query = connection.query("SELECT id, name, photo, 
timestamp FROM db WHERE id = 5");

        for (vector<record>::iterator it = results.begin(); it != 
results.end(); ++it) {
            cout << "\t" << it->id;
            cout << "\t" << it->name;
            cout << "\t" << it->blob.length();
            cout << "\t" << it->blob.size();
            cout << "\t" << it->timestamp << endl;   

            ofstream file("c:\\temp\\5.jpg", ios::binary);
            file.write(reinterpret_cast<char*> (&it->blob), 
    catch (const mysqlpp::BadQuery& er) {
        // Handle any query errors
        cout << "QUERY ERROR: " << er.what() << std::endl;
        return 1;
    catch (const mysqlpp::Exception& er) {
        // Catch-all for any other MySQL++ exceptions
        cout << "GENERAL ERROR: " << er.what() << std::endl;
        return 1;

    return 0;
saving BLOB to a fileValeri Tishev7 Jan
  • Re: saving BLOB to a fileWarren Young7 Jan