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
Subjects
mysql openssl Question
mysql:it 's a db not a dbms how it 's possible?!
Does the binary log enabling affect the MySQL performances?
Strange behavior, Table Level Permission
FULLTEXT query format question
Preventing Duplicate Entries
Comparing and writing out BLOBS
Executing MySQL Commands From Within C Program
Can 't access mysql after kernel upgrade
Mysql 4 0 Oracle Stored Procedure Trigger Conversion
Downgrade Mysql from 4 to 3 23
MySQL Cluster Software
mysql test 4 1 fails with the gis test
ERROR 2002: Can 't connect to local MySQL server through socket
Getting Identity after INSERT
Update one field with more fields from another table
ERROR 1045: Access denied for user: 'root@localhost ' (Using
password: NO)
mysql have same function mthod as Oracle decode()
 
Search:  
Power your search with and, or, +, -, or "some phrase" operators.
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)