If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Inserting binary data into MySQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-09-06, 16:44
cshakmal cshakmal is offline
Registered User
 
Join Date: Nov 2006
Posts: 1
Inserting binary data into MySQL

Hi,

I'm having problems inserting binary data into MySQL using C. Here's a simple example to illustrate what I'm trying to do:

int intArray[4];

intArray[0] = 0;
intArray[1] = 1;
intArray[2] = 3;
intArray[3] = 4;


char* byteArray(new char[sizeof(intArray)]);
memcpy(byteArray, intArray, sizeof(intArray));
sprintf(query,"INSERT INTO blobTable VALUES('%s')\n", byteArray);

mysql_real_query (itsDBConnection, query, strlen(query));



When I run the above code, the query executes but the BLOB field is empty. I pinted out the query string and it reads: INSERT INTO blobTable VALUES('')

Any ideas? Thanks!
Reply With Quote
  #2 (permalink)  
Old 11-21-06, 10:42
snorp snorp is offline
Registered User
 
Join Date: Apr 2004
Location: Europe->Sweden->Stockholm
Posts: 71
Quote:
Originally Posted by cshakmal
<snip>
intArray[0] = 0;
<snip>
memcpy(byteArray, intArray, sizeof(intArray));
sprintf(query,"INSERT INTO blobTable VALUES('%s')\n", byteArray);
<snip>
Hi there cshakmal,

this might not really be a MySQL question and I might not remember my C as good as I should, but here goes: you set intArray[0] to 0. The sprintf() function scans the supplied argument, byteArray, which will contain a zero byte first. This is what sprintf() consider to be "end of string". All str* functions behave this way, since a byte with value of 0 denotes "end of string". In this particular case, sprintf() will (correctly) interpret your supplied "string" as the empty string.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On