1

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 
1
  • my bounty is just looking for the snippet of code to replace in the above code that will make it work with much larger blobs. No need to provide the entire program. what I have above works, just not for very large blobs Commented Oct 9, 2013 at 20:07

2 Answers 2

2
+25

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks... I will check this when I have the chance. Hopefully next week.
0

Where buffer is defined put:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.