home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a063 / 9.img / SAMPLE / DBLIB / EXAMPLE3.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-02  |  5.0 KB  |  187 lines

  1. /*
  2. **    example3.c
  3. **
  4. ** This example selects some information from the "pubs" database.
  5. ** It illustrates binding of both aggregate and compute results.
  6. **
  7. ** Note that this example will only work if the "pubs" database exists
  8. ** on your SQL Server. Consult the Installation Guide for information
  9. ** about installing the "pubs" database.
  10. */
  11.  
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sybfront.h>
  16. #include <sybdb.h>
  17.  
  18. #define PLEN 4 
  19. #define DATEPRINT 26
  20. #define MONEYPRINT 12
  21.  
  22. /* Function Prototypes */
  23. int             err_handler(DBPROCESS *, int, int, int, char *, char *);
  24. int             msg_handler(DBPROCESS *, DBINT, int, int, char *, char *, 
  25.                                         char *, DBUSMALLINT);
  26. int                 main();
  27.  
  28. main()
  29. {
  30.  
  31.     LOGINREC       *login;
  32.     DBPROCESS      *dbproc;
  33.  
  34.     /* Declare the datatypes for the columns in the table "titles". */
  35.  
  36.     DBINT          pcount;
  37.     DBINT          sales;
  38.     DBINT          salesavg;
  39.     DBINT          sumsale;
  40.     DBCHAR         date[DATEPRINT+1];
  41.     DBCHAR         price[MONEYPRINT+1];
  42.     DBCHAR         priceavg[MONEYPRINT+1];
  43.     DBCHAR         pubid[PLEN+1];
  44.     RETCODE        result_code; /* to hold the results of dbresults(). */
  45.     STATUS         row_code;    /* to hold the results of dbnextrow(). */
  46.  
  47.     /* Initialize DB-Library. */
  48.     if (dbinit() == FAIL)
  49.         exit(ERREXIT);
  50.  
  51.     /* Install the user-supplied error-handling and message-handling
  52.      * routines. They are defined at the bottom of this source file.
  53.      */
  54.     dberrhandle(err_handler);
  55.     dbmsghandle(msg_handler);
  56.  
  57.     /* Set up the login information. */
  58.  
  59.     login = dblogin();
  60.     DBSETLPWD(login, "server_password");
  61.     DBSETLAPP(login, "example3");
  62.  
  63.     dbproc = dbopen(login, NULL);
  64.  
  65.     /* Send a "use database" command. */
  66.  
  67.     dbuse(dbproc,"pubs");
  68.  
  69.     /* Put the SQL statement into the command buffer. */
  70.  
  71.     dbcmd(dbproc, "select pub_id, pubdate, price, avg(price), ytd_sales,");
  72.     dbcmd(dbproc, " avg(ytd_sales), sum(ytd_sales) from titles");
  73.     dbcmd(dbproc, " group by pub_id");
  74.     dbcmd(dbproc, " order by pub_id");
  75.     dbcmd(dbproc, " compute count(pub_id) by pub_id");
  76.  
  77.     /* Send the command buffer to SQL Server for execution. */
  78.  
  79.     dbsqlexec(dbproc);
  80.  
  81.     /*
  82.     ** Using the aggregates "sum" and "avg" with the COMPUTE clause 
  83.     ** necessitates special handling when binding the results. Since each
  84.     ** aggregate creates a new column, this is accounted for in the bind.
  85.     ** Notice that avg(price) is the fourth column in the select-list,
  86.     ** and is also specified as the fourth column in the dbbind() routine.
  87.     **
  88.     ** The COMPUTE clause creates a compute row, which requires a 
  89.     ** special bind routine called dbaltbind().
  90.     */
  91.  
  92.     while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS)
  93.     {
  94.         if (result_code == SUCCEED)
  95.         {
  96.             dbbind(dbproc, 1, NTBSTRINGBIND, (DBINT)0, pubid);
  97.             dbbind(dbproc, 2, NTBSTRINGBIND, (DBINT)0, date);
  98.             dbbind(dbproc, 3, NTBSTRINGBIND, (DBINT)0, price);
  99.             dbbind(dbproc, 4, NTBSTRINGBIND, (DBINT)0, priceavg);
  100.             dbbind(dbproc, 5, INTBIND, (DBINT)0, (BYTE *)&sales);
  101.             dbbind(dbproc, 6, INTBIND, (DBINT)0, (BYTE *)&salesavg);
  102.             dbbind(dbproc, 7, INTBIND, (DBINT)0, (BYTE *)&sumsale);
  103.  
  104.             /* dbaltbind() binds compute columns. */
  105.             dbaltbind(dbproc, 1, 1, INTBIND, (DBINT)0, (BYTE *)&pcount);
  106.  
  107.             printf("\nAccounts:\n");
  108.             printf("---------\n\n");
  109.             printf
  110.                 ("%-5s  %-19s  %-6s  %-10s  %-5s  %-10s  %-10s\n\n",
  111.                  "pubid", "date", "price", "avg(price)",
  112.                  "sales", "avg(sales)", "sum(sales)");
  113.  
  114.             /*
  115.             ** Print out each result row, using different statements
  116.             ** depending on whether the row is a regular row or a 
  117.             ** compute row.
  118.             */
  119.  
  120.             while ((row_code = dbnextrow(dbproc)) != NO_MORE_ROWS)
  121.             {
  122.                 if (row_code == REG_ROW)
  123.                 {
  124.                     printf
  125.                         ("%5s  %19s  %6s  %10s  %5ld  %10ld  %10ld\n",
  126.                          pubid, date, price, priceavg, sales, 
  127.                          salesavg, sumsale);
  128.                 }
  129.                 else
  130.                     printf("title count:  %ld\n\n",pcount); 
  131.             }
  132.         }
  133.     }
  134.  
  135.     dbexit(  );
  136.     exit(STDEXIT);
  137. }
  138.  
  139. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  140. DBPROCESS       *dbproc;
  141. int             severity;
  142. int             dberr;
  143. int             oserr;
  144. char            *dberrstr;
  145. char            *oserrstr;
  146. {
  147.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  148.         return(INT_EXIT);
  149.     else 
  150.     {
  151.         printf("DB-Library error:\n\t%s\n", dberrstr);
  152.  
  153.         if (oserr != DBNOERR)
  154.             printf("Operating-system error:\n\t%s\n", oserrstr);
  155.  
  156.         return(INT_CANCEL);
  157.     }
  158. }
  159.  
  160. int msg_handler(dbproc, msgno, msgstate, severity, msgtext, 
  161.                 srvname, procname, line)
  162.  
  163. DBPROCESS       *dbproc;
  164. DBINT           msgno;
  165. int             msgstate;
  166. int             severity;
  167. char            *msgtext;
  168. char            *srvname;
  169. char            *procname;
  170. DBUSMALLINT     line;
  171.  
  172. {
  173.     printf ("Msg %ld, Level %d, State %d\n", 
  174.             msgno, severity, msgstate);
  175.  
  176.     if (strlen(srvname) > 0)
  177.         printf ("Server '%s', ", srvname);
  178.     if (strlen(procname) > 0)
  179.         printf ("Procedure '%s', ", procname);
  180.     if (line > 0)
  181.         printf ("Line %d", line);
  182.  
  183.     printf("\n\t%s\n", msgtext);
  184.  
  185.     return(0);
  186. }
  187.