Pregunta

I have a mysql table with a longblob field. I have successfully inserted the blob records into the table and extracted them back into files on disk using python.

Now I want to select the latest version blob from the table and write it to a file on disk using c++ and mysql connector. This is the code I have so far.

It appears to work for smaller blobs, but not enough for large blobs on my database. The max buffer size appears to be near 1000000.

How can I write large longblob files from the mysql select?

string appname = "rocket.exe"; 

driver = get_driver_instance();
con = driver->connect(mysql_server, mysql_user, mysql_password);
con->setSchema("app");
stmt = con->createStatement();

sqlstring =
    "select installer"
    "  from app_table"
    " where appname = '" + appname + "'"
    "   order by version desc "
    " limit 1";  

std::istream *blob;
res = stmt->executeQuery(sqlstring);
while (res->next()) {
    blob = res->getBlob("installer");
}

char buffer[1000000];
memset(buffer, '\0', 1000000);
blob->read((char*)buffer,1000000);   

std::ofstream outfile ("rocket2.exe",std::ofstream::binary);
outfile.write (buffer,1000000);
outfile.close();

delete res;   // resultset
delete stmt;  // statement
delete con;   // connection 
¿Fue útil?

Solución

That's a large buffer. You are probably running out of stack space. Allocate to the heap by adding some memory management or using vector. You said you can use about 1000000. The variability there is due to the stack space being used by the rest of the code.

#include <vector>;
std::vector<char> buffer(1000000, '\0');
blob->read(&buffer[0], buffer.size());

For more information on stack vs heap memory, look here, and here

Otros consejos

Where buffer is defined put:

std::vector<char> buffer(1000000, '\0');
blob->read(&buffer[0], buffer.size());  
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top