Hi all !
I would like to insert a Blob ( a picture exactly ), here is a small
source code to test my insert.
You can find 2 sources code, the first is to insert a blob in a table of
a database ( this database exists in my MySQL server )
And the second code is to read this blob from the database.
I think there are a lot of mistakes, could you help me ? because i did
not find the solution.
thanks
#include mysql/mysql.h
#include stdio.h
#include stdlib.h
#include string.h
#include unistd.h
#include sys/stat.h
#include fcntl.h
#include glib.h
int main(int argc, char **argv)
{
MYSQL *mysql = NULL;
struct stat statbuf;
int file;
unsigned long size, ssize;
gchar *data = NULL, *query = NULL, *end = NULL;
int i;
mysql = (MYSQL *) g_malloc0(sizeof(MYSQL));
mysql_init(mysql);
mysql = mysql_real_connect(mysql, localhost,test, ,
test_stef, 0, NULL, 0);
file = open(photo.png, O_RDONLY);
//file = open(test_mozilla.c, O_RDONLY);
if ( !file )
g_error(Impossible d'ouvrir le fichier photo.png);
fstat(file, statbuf);
fprintf(stdout, Size of photo.png : %ld\n, statbuf.st_size);
size = statbuf.st_size;
data = (gchar *) g_malloc0(size);
if ( !data )
g_error(Impossible de charger la mémoire requise);
ssize = read(file, data, size);
if ( ssize == size )
g_message(Fichier correctement lu);
query = (gchar *) g_malloc0(2*size);
strcpy(query, INSERT INTO test VALUES();
end = query + strlen(query);
*end++ = '\'';
gchar * debut, *fin;
debut = end;
end += mysql_real_escape_string(mysql, end, data, size);
fin = end;
fprintf(stdout, Difference : %lu\n,(unsigned long) (fin - debut));
*end++ = '\'';
*end++ = ')';
/*for ( i = 0 ; i 30 ; i++ )
fprintf(stdout, %c, query[i]);*/
if ( mysql_real_query(mysql, query, (unsigned long) ( end - query )) )
{
g_error(Erreur lors de la requete d'insertion dans la DB);
}
g_free(data);
g_free(query);
mysql_close(mysql);
return 0;
}
But when i edit the mysql database with mysqlcc utility, i don't the
complete file.
now, when i read the Blob from the database, i use this source code.
#include glib.h
#include mysql/mysql.h
#include stdio.h
#include string.h
#include stdlib.h
int main(int argc, char **argv)
{
MYSQL *mysql = NULL;
MYSQL_RES *mysql_res = NULL;
MYSQL_FIELD *mysql_field = NULL;
MYSQL_ROW *mysql_row = NULL;
gchar query[] = SELECT photo FROM test;
mysql = (MYSQL *) g_malloc(sizeof(MYSQL));
mysql_init(mysql);
if( !mysql_real_connect(mysql, localhost, test, ,
test_stef,0, NULL, 0) )
g_error(Failed to connect to database : Error : %s\n,
mysql_error(mysql));
if (mysql_real_query(mysql, query, strlen(query)))
g_error(Failed query to database : Error : %s\n,
mysql_error(mysql));
if ( !(mysql_res = mysql_store_result(mysql)) )
g_error(Failed Store Result : Error : %s\n, mysql_error(mysql));
mysql_field = mysql_fetch_field(mysql_res);
g_print(Field name : %s\n, mysql_field-name);
mysql_row = mysql_fetch_row(mysql_res);
FILE *fichier;
fichier = fopen(photo_2.png, wb);
fwrite(mysql_row[0], strlen(mysql_row[0]), 1, fichier);
fclose(fichier);
mysql_close(mysql);
g_free(mysql);
return 0;
}
Thanks
Stephane Wirtel
-
Before posting, please check:
http://www.mysql.com/manual.php (the manual)
http://lists.mysql.com/ (the list archive)
To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php