문제

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 
도움이 되었습니까?

해결책

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

다른 팁

Where buffer is defined put:

std::vector<char> buffer(1000000, '\0');
blob->read(&buffer[0], buffer.size());  
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top