| Mailing List | | Home | | MySQL General - General MySQL discussion | | MaxDB - Everything about MaxDB, formerly known as SAP DB | | MySQL on Win32 - Runing MySQL on Windows 9x/Me/NT/2000/XP | | Java Help - Mostly related to the MySQL Connector/J driver | | ODBC - ODBC with the MySQL Connector/ODBC driver | | Perl - Perl support for MySQL with DBI and DBD::mysql | | MySQL++ - Programming with the C++ API to MySQL |
|
|
  | |  | Comparing and writing out BLOBS | Comparing and writing out BLOBS 2004-03-19 - By John Ling
Back I had posted the following code in the plusplus mailing list but it was
suggested I post this question in this list instead.
I 've been writing a test program using the MySQL C API to test the
reading and writing of BLOB data in and out of the database. The file I
read into the database is a small binary program. I then write out this
BLOB to disk as another file. There is obviously something wrong as the
newly written out file does not execute when run:
"./a.out: Exec format error. Binary file not executable. "
Permissions are executable. I think the code that I use to read in the
BLOB is correct. But I am not sure if I am retrieving the BLOB
properly. I want to do a comparison between my input buffer and what I
get back from a mysql_fetch_row() but I 'm not sure if I am doing it
correctly or not. If I am doing the comparison correctly, then there is
something wrong because the comparison between the two buffers fails at
some point (the first 630 some characters are the same but then a
failure), in which case, what am I doing wrong? How do I read out the
program that I stored in the database?
I give my code here and I would much appreciate it if someone could
point out what it is I am not doing correctly.
// First read in the BLOB into the database
ifstream is;
is.open(inputFilename,ifstream::in|ifstream::binary);
is.seekg(0, ifstream::end);
long bufferSize = is.tellg();
is.seekg(0, ios::beg);
char buffer[bufferSize];
is.read(buffer, bufferSize);
is.close();
// Set query string
char mySql[55000] = "INSERT into Blob_file (blob_file) values ( ' ";
char* tail;
tail = mySql + strlen(mySql);
if ((tail + 2*bufferSize) + 3 > mySql + sizeof(mySql)) {
cerr < < "Binary too big " < < endl;
return 1;
}
tail += mysql_escape_string(tail, buffer, bufferSize);
(void) strcpy (tail, " ') ");
mysql_real_query(myConnection, mySql, strlen(mySql));
// Now try retrieve the BLOB out of the database
char getMySql[1024] = "select blob_file from Blob_file where
blob_file_id = 79435 "; // assuming the insert created entry 79435
mysql_real_query(myConnection, getMySql, strlen(getMySql));
MYSQL_RES* Res;
MYSQL_ROW Row;
Res = mysql_store_result(myConnection);
cout < < mysql_num_rows(Res) < < endl;
Row = mysql_fetch_row(Res);
unsigned long *lengths;
lengths = mysql_fetch_lengths(Res);
// Is what I retrieved the same as what I put in?
for (int j=0;j <=lengths[0]-1;j++) {
cout < < ". ";
if (Row[0][j] != buffer[j]) {
cout < < "BAD " < < endl;
return 1; // I end up here and terminate so something wrong
};
}
ofstream os;
os.open(outputFilename,ofstream::binary);
os.write(Row[0],bufferSize);
os.close();
// close database connection
myDatabase.disconnect();
return 0;
Thank You,
John
--
MySQL General Mailing List
For list archives:
http://lists.mysql.com/mysql
To unsubscribe:
http://lists.mysql.com/mysql?unsub=mysql
@(protected)
|
|
 |