Unable to Insert Binary data in to mysql -
03-05-2006
, 10:19 PM
------=_Part_8564_11315953.1141618765869
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
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=3D0,r=3D0,t,n=3D0;
int des[5];
//getting data from a file which contains finger print template.
flag =3Dfopen("/home/haranath/mysql/mysql_samples/hara","rb");
//here we r having 102 bytes of binary data.
if(flag!=3DNULL)
{
from_str=3D(char *)malloc(300*sizeof(char));
des[0]=3Dfread(from_str,1,256,flag);
fclose(flag);
}
else {
printf("fopen failed\n");
exit(1);
}
//initialising mysql
mysql =3D mysql_init(NULL);
//connecting to mysql client
mysql_real_connect(mysql,NULL,NULL,NULL,"biometric ",0,NULL,0);
from_len=3Ddes[0];
to_str=3D(char *)malloc(600);
//escaping the binary data to human readable data
to_len =3Dmysql_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=3D(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=3D(char *)malloc(50);
query=3D"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 =3D mysql_store_result(mysql);
while((record =3D 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
------=_Part_8564_11315953.1141618765869-- |