I have an Insert function which opens image file and inserts to database.
void insertBlob()
{
sql::Driver *driver;
sql::Connection *con;
sql::PreparedStatement *pstmt;
ifstream file ("/home/malper/NetBeansProjects/BlobTesting/2", ios::in | ios::binary | ios::ate);
ifstream::pos_type fileSize;
char* fileContents;
char buff[1024];
if(file.is_open())
{
fileSize = file.tellg();
fileContents = new char[fileSize];
file.seekg(0, ios::beg);
if(!file.read(fileContents, fileSize))
{
cout << "fail to read" << endl;
}
file.close();
}
istringstream str(fileContents);
/////////////////////////////////////////////////
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://192.168.2.5:3306", "root", "root");
/* Connect to the MySQL test database */
con->setSchema("test");
pstmt=con->prepareStatement("INSERT INTO Dao VALUES(57,4,'2014-12-23 11:55:54',?,1,1)");
pstmt->setBlob(1,&str);
pstmt->executeUpdate();
}
Database part works well but i can't get binary image to stringstream after open file. How can i store binary in a string in this function?