Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2006
    Posts
    1

    Unanswered: 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!

  2. #2
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •