Using descriptors to select blob data
Posted in 1995
Topics: Stored Procedures & SPL, Error Codes & Troubleshooting, Data Types & Schema Design
/* I am having a problem using descriptors to select blob data from the database. You can select everything else but blob data as far as I can tell with descriptors. We can not use a direct select into clause because we are using objects at run time and don't know the parameters. The part of code below that is commented out is what doesnt work. Is there anyone who has done this before or has any suggestions? The error I get is -450. Kristi * getcd_me.ec * The following program reads the dbm_text column into memory and displays it. */ #include <stdio.h> $include sqltypes.h; $include locator.h; #define LCASE(c) (isupper(c) ? tolower(c) : c) #define BUFFSZ 256 #define DBM_DESCRIPTOR_SIZE 25 char errmsg[512]; $char db_stmnt[250]; $long cat_num; $short stock_num; $char manu_code[4]; $loc_t cat_descr; $loc_t cat_picture; $varchar cat_advert[256]; $static char descrip_name[] = "select_descrp"; /* descriptor name */ $static int descriptor_size = 2; $char sql_prep_name[1500]; $char qry_curs[100]; $char sql_string[1500]; $loc_t dbm_text; main(int argc, char *argv[]) { int length; char ans[BUFFSZ]; int stat=0; exec sql database almrs_b3; while (1) { printf("\\n\\tEnter text_id: "); /* prompt for catalog number */ getans(ans, 6); if (rstol(ans, &cat_num)) /* cat_num string to long */ { printf("\\n\\tIllegal cat_num %s", ans); continue; } /* * Prepare locator structure for select of dbm_text */ dbm_text.loc_loctype = LOCMEMORY; /* set loctype for in * memory */ dbm_text.loc_bufsize = -1; /* let db get buffer */ dbm_text.loc_oflags = 0; /* clear loc_oflags */ strcpy(sql_string, "Select gb_mb_srvy_txt from d_bounds_survey844 " "where geo_loc_id = 1"); EXEC SQL PREPARE sql_prep_name from :sql_string; stat = sqlca.sqlcode; printf("prepare stat: %d\\n", stat); EXEC SQL DECLARE qry_curs cursor for sql_prep_name; stat = sqlca.sqlcode; printf("declare stat: %d\\n", stat); /************************************ EXEC SQL ALLOCATE DESCRIPTOR :descrip_name with max :descriptor_size; stat = sqlca.sqlcode; printf("allocate descriptor stat: %d\\n", stat); EXEC SQL DESCRIBE sql_prep_name using sql descriptor :descrip_name; stat = sqlca.sqlcode; printf("describe stat: %d\\n", stat); ************************************/ EXEC SQL OPEN qry_curs; stat = sqlca.sqlcode; printf("open stat: %d\\n", stat); EXEC SQL FETCH qry_curs into :dbm_text; stat = sqlca.sqlcode; printf("fetch stat: %d\\n", stat); /******************************* EXEC SQL FETCH qry_curs using sql descriptor :descrip_name; stat = sqlca.sqlcode; printf("fetch stat: %d\\n", stat); ************************************/ if (!err_chk("Select")) /* if not found */ { printf("\\n\\t** Cat_num %ld not found in catalog table **", cat_num); if (!more()) /* More to do? */ break; /* no, terminate loop */ else continue; /* yes */ } prdesc(); /* if found, print dbm_text */ if (!more()) /* More to do? */ break; /* no, terminate loop */ } } /* Accept input up to the length specified by len. */ getans(ans, len) char *ans; int len; { char buf[BUFFSZ]; int c, n = 0; while ((c = getchar()) != '\\n') buf[n++] = c; buf[n] = '\\0'; if (n > 1 && n >= len) { printf("Input exceeds maximum length"); return 0; } if (len <= 1) *ans = buf[0]; else strncpy(ans, buf, len); return 1; } /* prdesc() prints cat_desc for a row in the catalog table */ prdesc() { long size; char shdesc[81], *p; size = dbm_text.loc_size; /* get size of data */ printf("\\nDescription for %ld:\\n", cat_num); p = dbm_text.loc_buffer; /* set p to buffer addr */ /* * print buffer 80 characters at a time */ while (size >= 80) { ldchar(p, 80, shdesc); /* mv from buffer to shdesc */ printf("\\n%80s", shdesc); /* display it */ size -= 80; /* decrement length */ p += 80; /* bump p by 80 */ } strncpy(shdesc, p, size); shdesc[size] = '\\0'; printf("%-s\\n", shdesc); /* display last segment */ } /* Ask user if there is more to do */ more() { char ans; do { printf("\\n\\tMore? (y/n) . . ."); getans(&ans, 1); } while ((ans = LCASE(ans)) != 'y' && ans != 'n'); return (ans == 'n') ? 0 : 1; } /* err_chk() checks sqlca.sqlcode and if an error has occurred, it uses rgetmsg() to display the message for the error number in sqlca.sqlcode. */ err_chk(char *name) { if (sqlca.sqlcode < 0) { rgetmsg((short) sqlca.sqlcode, errmsg, sizeof(errmsg)); printf("\\n\\tError %d during %s: %s\\n", sqlca.sqlcode, name, errmsg); exit(1); } return ((sqlca.sqlcode == SQLNOTFOUND) ? 0 : 1); }