ex20.c

Example for using sqlo_query_result.

00001 /* $Id: ex20.c 301 2005-01-08 17:28:49Z kpoitschke $ */
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include "examples.h"
00005 
00006 /* Select employees with salary > min_income using sqlo_query_result */
00007 
00008 int ex20(sqlo_db_handle_t dbh, double min_income)
00009 {
00010   sqlo_stmt_handle_t sth;       /* statement handle */
00011   int status;                   /* status of sqlo calls */
00012   unsigned int i,j;                      /* loop counter */
00013   char ** v;                    /* values */
00014   char ** col_names;           /* column names */
00015   unsigned int *nl;            /* column name lengths */
00016   unsigned short *vl;           /* value lengths */
00017   unsigned int nc;              /* number of columns */
00018   int nrows;
00019 
00020   sth = reopen_cursor(dbh, min_income); /* see example of sqlo_reopen (ex6.c) */
00021 
00022 
00023   printf("Employees with SAL > %-8.2f:\n", min_income);
00024 
00025 
00026   nrows = 0;
00027   /* fetch the data */
00028   while ( SQLO_SUCCESS == (status = (sqlo_query_result(sth, /* statement */
00029                                &nc, /* number of columns */
00030                                &v,  /* values */
00031                                &vl, /* value lengths */
00032                                &col_names, /* column names */
00033                                &nl   /* column name lengths */
00034                                )))) {
00035     
00036     if ( nrows == 0 )
00037       {
00038     /* print the header */
00039     for (i = 0; i < nc; ++i)
00040       printf("%-*s ", nl[i]+4, col_names[i]);
00041     printf("\n");
00042     for (i = 0; i < nc; ++i) {
00043       for (j = 0; j < nl[i]+4; ++j) {
00044         putchar('-');
00045       }
00046       putchar('+');
00047     }
00048     putchar('\n');
00049       }
00050     
00051     /* print the column values */
00052     for (i = 0; i < nc; ++i)
00053       printf("%-*s ", (vl[i] > nl[i] ? vl[i] : nl[i])+4, v[i]);
00054     
00055     printf("\n");
00056 
00057     ++nrows;
00058   }
00059 
00060   if (0 > status) {
00061     error_exit(dbh, "sqlo_query_result");
00062   }
00063 
00064   if ( SQLO_SUCCESS != sqlo_close(sth))
00065     error_exit(dbh, "sqlo_close");
00066 
00067   return 1;
00068 
00069 }
00070 
00071 /* $Id: ex20.c 301 2005-01-08 17:28:49Z kpoitschke $ */
00072    

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