Re: Fetching a blob field
Posted in 1999
Hello,
I think what your program is missing is the setting of the loc_t field loc_size.
From the ESQL/C (V 9.13) manual p 8-9:
"...
loc_size long
Contains the size of the simple-large-object data in
bytes. This field indicates the amount of simple-large-
object data that the ESQL/C libraries read or
write. The ESQL/C program sets loc_size when it in-serts
a simple large object in the database; the
ESQL/C libraries set loc_size after it selects or fetches
a simple large object.
..."
R Smith wrote:
> I want to be able to store and fetch blobs via the LOCMEMORY
> method. These, what I thought were simple routines, do not
> appear to be working.
>
> The INSERT appears to work, but when I attempt to verify the
> data, it shows nothing in the blobvar (blob) column. I don't
> know if the FETCH is broken, too.
>
> Can anyone tell me what I'm doing wrong?
>
> Thank you.
>
> #include <stdlib.h>
>
> EXEC SQL INCLUDE sqlca;
> EXEC SQL INCLUDE locator;
> EXEC SQL INCLUDE sqltypes;
>
> #define BLOB_INSERT 0
> #define BLOB_FETCH 1
>
> void SetBlob (loc_t *locator, int mode, char *blobbuff);
>
> int main (int argc, char **argv)
> {
> EXEC SQL BEGIN DECLARE SECTION;
> int id;
> loc_t testb;
> char dbname[20];
> EXEC SQL END DECLARE SECTION;
> int i;
> static char *ptr = "check this out";
> char buff[50];
>
> strcpy (dbname, "mydb");
> EXEC SQL DATABASE :dbname;
>
> fprintf (stderr, "Loading recs\\n\\n");
> for (i = 0; i < 10; i++)
> {
>
> sprintf (buff, "%s: %d", ptr, i);
> SetBlob (&testb, BLOB_INSERT, buff);
>
> EXEC SQL INSERT INTO blobtbl (blobvar) values (:testb);
> }
>
> fprintf (stderr, "Getting recs\\n\\n");
> EXEC SQL DECLARE mycurs CURSOR FOR
> SELECT id, blobvar
> FROM blobtbl;>
> EXEC SQL OPEN mycurs;
>
> for ( ; ; )
> {
> memset (buff, '\\0', sizeof (buff));
> SetBlob (&testb, BLOB_FETCH, (char *) 0);
> EXEC SQL FETCH mycurs INTO :id, :testb;
> if (sqlca.sqlcode)
> break;
> fprintf (stderr, "id = %d, text = '%s', [%d]\\n", id,testb.loc_buffer,
> (int) sqlca.sqlcode);
> }
> exit (1);
> }
>
> void SetBlob (loc_t *locator, int mode, char *blobbuff)
> {
> memset (locator, '\\0', sizeof (loc_t));
> locator->loc_loctype = LOCMEMORY;
> locator->loc_indicator = 0;
> locator->loc_buffer = blobbuff;
> switch (mode)
> {
> case BLOB_FETCH:
> locator->loc_bufsize = -1;
> break;
> case BLOB_INSERT:
> locator->loc_buffer = blobbuff;
> locator->loc_bufsize = strlen (blobbuff);
> break;
> }
>
> locator->loc_type = SQLTEXT;
> }
>
> -----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
> http://www.newsfeeds.com The Largest Usenet Servers in the World!
> ------== Over 73,000 Newsgroups - Including Dedicated Binaries Servers ==-----