ex12.c

Example for fetching data into an array of structures.

00001 /* $Id: ex12.c 221 2002-08-24 12:54:47Z kpoitschke $ */
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include "examples.h"
00005 
00006 enum {
00007   MAX_NAME_LEN = 64,
00008   MAX_ARRAY_SIZE = 4            /* Note: usually this should be 100 or so. 
00009                                  * Used a smaller one to do test all paths in the code
00010                                  */
00011 };
00012 
00016 typedef struct _result_t {
00017   char name[MAX_NAME_LEN+1];    /* output ENAME */
00018   double salary;                /* output SAL */
00019   short name_ind;               /* indicator variable of ENAME */
00020   short sal_ind;                /* indicator variable of SAL */
00021   unsigned short name_rlen;     /* the actual length of ENAME */
00022   unsigned short name_rcode;    /* the return code of ENAME */
00023 } result_t;
00024 
00025 void print_record __P((result_t * r));
00026 
00027 
00028 int do_array_select(sqlo_db_handle_t dbh, double min_salary)
00029 {
00030   sqlo_stmt_handle_t sth;       /* statement handle */
00031   int i;                        /* loop variable */
00032   int status;                   /* return code of sqlo_... */
00033   result_t result[MAX_ARRAY_SIZE];
00034   int rows_fetched;             /* number of rows fetched per execute */
00035   int rows_fetched_total = 0;   /* total number of rows */
00036   int done_fetching = 0;        /* flag indicating end of fetch */
00037   double salary = min_salary;   /* input variable for SAL */
00038   int skip_size = sizeof(result[0]); /* The skip size */
00039 
00040   sth = prepare_cursor(dbh, &salary); /* see ex10.c */
00041 
00042   /* define output */
00043   if (SQLO_SUCCESS != 
00044       (sqlo_define_by_pos2(sth, 1, SQLOT_STR, result[0].name, sizeof(result[0].name), 
00045                            &result[0].name_ind, &result[0].name_rlen, 
00046                            &result[0].name_rcode, skip_size)) ||
00047       (sqlo_define_by_pos2(sth, 2, SQLOT_FLT, &result[0].salary, sizeof(result[0].salary),
00048                            &result[0].sal_ind, 0, 0, skip_size))) {
00049     error_exit(dbh, "sqlo_define_by_pos");
00050   }
00051 
00052   /* execute and fetch  the result */
00053   status = sqlo_execute(sth, 0);
00054 
00055   while (!done_fetching) {
00056 
00057     rows_fetched = MAX_ARRAY_SIZE;
00058 
00059     /* get the next set */
00060     status = sqlo_fetch(sth, MAX_ARRAY_SIZE);
00061 
00062     if (0 > status)
00063       error_exit(dbh, "sqlo_execute(NEXT)");
00064     else if (SQLO_NO_DATA == status) {
00065       
00066       rows_fetched = sqlo_prows(sth);
00067           
00068       /* sqlo_prows returns now the total number of fetched rows
00069        * the difference to the previous total fechted rows is
00070        * the number of rows fetched in this last call to sqlo_execute
00071        */
00072       rows_fetched = rows_fetched - rows_fetched_total;
00073       done_fetching = 1;
00074 
00075     }
00076     
00077     /* print the records */
00078     for (i = 0; i < rows_fetched; ++i)
00079       print_record(&result[i]);
00080 
00081     rows_fetched_total += rows_fetched;
00082 
00083   }
00084   printf("Selected %d employees\n", rows_fetched_total);
00085   /* finished. */
00086   sqlo_close(sth);
00087 
00088   return 1;
00089 }
00090 
00091 /* print record */
00092 void print_record(result_t * r /* I - The record */ )
00093 {
00094   printf("Name=%-8s Salary= %6.2f\n",
00095          (r->name_ind == SQLO_NULL_IND ? "NULL" : r->name),
00096          (r->sal_ind == SQLO_NULL_IND ? -1.0 : r->salary));
00097 }
00098 
00099 /* $Id: ex12.c 221 2002-08-24 12:54:47Z kpoitschke $ */

Generated on Mon May 21 13:43:28 2007 for libsqlora8 by  doxygen 1.4.7