Thanks for your quick response!
That's just what I needed.
The secret is using mysql_escape_string to change the data such that it will
not cause problems with delimiters!
Thank You!
> "Have A Great Scouting Day"
> Ken Hylton
> 7826 Falcon Ridge Drive
> San Antonio, Texas 78239-4032
> 210-646-9508 (home)
> 210-949-7261 (work)
> 210-949-7254 (fax)
> 210-287-6756 (cell)
> [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
> [EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]>
-----Original Message-----
From: paradoxix [mailto:[EMAIL PROTECTED]]
Sent: Thursday, February 28, 2002 3:04 PM
To: Kenneth Hylton
Cc: [EMAIL PROTECTED]
Subject: Re: INSERT blob data in C
just some code I did some time ago:
/* */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/fcntl.h>
#include <mysql/mysql.h>
MYSQL dbcon;
int main(int argc, char *argv[])
{
int i;
char *tmpQ=malloc(2*1024*1024);
char *end;
int file;
int size;
char *mem;
char tmpstr[1024];
mysql_init(&dbcon);
mysql_real_connect(&dbcon, NULL, "username", "password",
"dbname",0,"/tmp/mysql.sock",0);
for(i=1;i<argc;i++) {
file = open(argv[i], O_RDONLY);
lseek(file, 0, SEEK_SET);
size = lseek(file, 0, SEEK_END);
lseek(file, 0, SEEK_SET);
mem = malloc(size);
read(file,mem,size);
printf("inserting: %s\n",argv[i]);
end = (char *) strmov(tmpQ,"INSERT INTO image values(");
*end++ = '\'';
sprintf(tmpstr,"%i",i);
end = strmov(end, tmpstr);
*end++ = '\'';
*end++ = ',';
*end++ = '\'';
sprintf(tmpstr,"%i",i);
end = strmov(end, tmpstr);
*end++ = '\'';
*end++ = ',';
*end++ = '\'';
end += mysql_escape_string(end, mem, size);
*end++ = '\'';
*end++ = ')';
mysql_real_query(&dbcon, tmpQ, (unsigned int) (end -
tmpQ));
free(mem);
close(file);
}
mysql_close(&dbcon);
exit(0);
return 0;
}
Kenneth Hylton wrote:
> Howdy -
>
> I looked in the doc @ mysql.com and the New Riders and
O'Reilly MySQL books.
>
> Plus, I consulted the archives as best I could and did not
see an answer for
> this:
>
> How do you insert binary data using the C API?
>
> I know you need to use mysql_real_query method and pass
length, but how do
> you delimit the blob field?
>
> INSERT INTO some_table (key_field,blob_field) VALUES (
'A', '<binary
> data>');
> INSERT INTO some_table SET key_field = 'A', blob_field =
'<binary data>';
>
> Both have the issue of having the single quote appear in
the data.
>
> These fields I'm storing are between 10 and 2000 bytes
long and there are
> millions of them so I can't store them as separate files
and then use the
> file name like I would for a graphic or downloadable file
on a web page.
>
> TIA!
>
> > "Have A Great Scouting Day"
> > Ken Hylton
> > 7826 Falcon Ridge Drive
> > San Antonio, Texas 78239-4032
> > 210-646-9508 (home)
> > 210-949-7261 (work)
> > 210-949-7254 (fax)
> > 210-287-6756 (cell)
> > [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
> > [EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED]>
>
>
---------------------------------------------------------------------
> 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
---------------------------------------------------------------------
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