List:MySQL++« Previous MessageNext Message »
From:hara Date:March 6 2006 4:19am
Subject:Unable to Insert Binary data in to mysql
View as plain text  
 Hai all,
I am haranath.t and this is my 1st post to this group.I am very much new to
mysql database.Here i want to
insert the finger print template's(Binaray data) into mysql database
from C API calls.I have been stucked up at this point  from couple of
weeks,any body having idea on this plz help me out.Here i am giving my
code.

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include<string.h>
#include </usr/include/mysql/mysql.h>
#if !defined(MYSQL_VERSION_ID) || (MYSQL_VERSION_ID<32314)
#define mysql_real_escape_string(mysql,to_str,from_str,len) \
              mysql_escape_string(to_str,from_str,len)
#endif

//mysql variables

  MYSQL *mysql;
  FILE *flag;
  MYSQL_RES  *results;
  MYSQL_ROW  record;
  char *InsQuery;
  char *query;
  unsigned long to_len,from_len;
  unsigned char *to_str,*from_str;
       int main(void)
        {
         FILE *flag;
         int j=0,r=0,t,n=0;
         int des[5];
         //getting data from a file which contains finger print template.

         flag =fopen("/home/haranath/mysql/mysql_samples/hara","rb");

//here we r having 102 bytes of binary data.

         if(flag!=NULL)
         {
          from_str=(char *)malloc(300*sizeof(char));
            des[0]=fread(from_str,1,256,flag);
          fclose(flag);
          }
          else {
               printf("fopen  failed\n");
               exit(1);
               }

//initialising mysql

   mysql = mysql_init(NULL);

//connecting to mysql client

  mysql_real_connect(mysql,NULL,NULL,NULL,"biometric",0,NULL,0);
   from_len=des[0];
   to_str=(char *)malloc(600);

//escaping the binary data to human readable data

   to_len =mysql_real_escape_string(mysql,to_str,from_str,from_len);

//to_str contains 105 bytes(2 for replacement of NULL with \0 and 1
for termination charecter)

   printf("encrypted data:%s\t enc data len %d\n",to_str,to_len);
   InsQuery=(char *)malloc(to_len+255);

//fomating the query before processing
   sprintf(InsQuery,"insert into sample values('%s')",to_str);
   printf("Insquery:%s\n",InsQuery);
   printf("len of query%d\n",strlen(InsQuery));

//making query to mysql

   if(mysql_real_query(mysql,InsQuery,strlen(InsQuery))){
   printf("Error making insquery: %s\n",mysql_error(mysql));
   exit(1);
   }
   else{
         printf("insQuery made...\n");
         free(InsQuery);
   }
   query=(char *)malloc(50);
   query="select * from sample";

//sample is the table in biometric database
//query for retriving the data from database

   if(mysql_query(mysql,query)){
   printf("Error Making Query: %s\n",mysql_error(mysql));
   exit(1);
   }
   else{
           printf("query made...\n");
         free(query);
   }
    //Retriving the data from database
   results = mysql_store_result(mysql);
   while((record = mysql_fetch_row(results))) {
   printf("%s\n",record[0]);
   }


RESULTS:
Here,the data before the 1st null charecter is getting in to mysql
database.
mysql_real_query is for processing Binary data but it is behaving
like mysql_query only.

In my database i have decleared binary data field as binary(255).I have
tried with blob too.

My development system is Mandriva linux LE 2005 and i have installed
mysql client and server from RPM's & my vesion is 5.0-18.

Hope i have explaned clearlly.
Thanks in advance and i will be waiting for all ur reponses guys..


                              With Regards,
                              Haranath.T

Thread
Unable to Insert Binary data in to mysqlhara6 Mar
  • Re: Unable to Insert Binary data in to mysqlWarren Young6 Mar