home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a063 / 13.img / SAMPLE / DBLIB / EXAMPLE3.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-13  |  4.2 KB  |  164 lines

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