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