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

  1. /*    example4.c */
  2. /*
  3. ** This example accesses the data within each row without using dbbind()
  4. ** and illustrates the use of row buffering.
  5. **
  6. ** It runs a query, saves all of the returned rows (up to a maximum
  7. ** of 100) using DB-LIBRARY row buffering, and allows the user to
  8. ** examine data rows at random.
  9. */
  10.  
  11. #define DBMSOS2
  12. #include <stdio.h>
  13. #include <sqlfront.h>
  14. #include <sqldb.h>
  15.  
  16. extern char    *malloc();
  17.  
  18. DBPROCESS    *dbproc;    /* Our connection with SQL Server. */
  19. LOGINREC    *login;    /* Our login information. */
  20.  
  21. #define STDEXIT 0
  22. #define TYPELEN 2
  23. #define DATELEN 25
  24.  
  25. /* Forward declarations of the error handler and message handler.
  26. */
  27. int    err_handler();
  28. int    msg_handler();
  29.  
  30. main(argc, argv)
  31. int    argc;
  32. char    *argv[];
  33. {
  34.     /* Here are the variables which will be used to store the
  35.     * data being examined.
  36.     */
  37.     DBCHAR    name[MAXNAME+1];    /* MAXNAME is defined in
  38.     * "sqldb.h" as the maximum
  39.     * length for names of database
  40.     * objects, such as tables,
  41.     * columns, and procedures.
  42.     */
  43.     DBCHAR    type[TYPELEN+1];
  44.     DBINT    id;
  45.  
  46.     DBCHAR    datebuf[64];
  47.     DBINT    len;
  48.     char    numstring[32];
  49.     int    quitflag = 0;
  50.     RETCODE    row_code;
  51.     DBINT    rownum;
  52.  
  53.     /* Install the user-supplied error-handling and message-handling
  54.     * functions. They are defined at the bottom of this source file.
  55.     */
  56.     dberrhandle(err_handler);
  57.     dbmsghandle(msg_handler);
  58.  
  59.     /*
  60.     ** Get a LOGINREC structure and fill it with the necessary
  61.     ** login information.
  62.     */
  63.  
  64.     login = dblogin();
  65.     DBSETLUSER (login, "user");
  66.     DBSETLPWD(login, "my_passwd");
  67.     DBSETLAPP(login, "example4");
  68.  
  69.     dbproc = dbopen(login, "my_server");
  70.     dbcmd(dbproc, "select name, type, id, crdate from sysobjects");
  71.     dbcmd(dbproc, " where type = 'S'");
  72.  
  73.     /*
  74.         ** Set row buffering to 100 rows, via dbsetopt().
  75.     ** Note that this parameter must be passed as an ASCII string.
  76.     */
  77.  
  78.     dbsetopt(dbproc, DBBUFFER, "100");
  79.     dbsqlexec(dbproc);
  80.     if (dbresults(dbproc) == SUCCEED)
  81.     {
  82.         /* Read all of the rows into DB-LIBRARY's buffer */
  83.         while ((row_code = dbnextrow(dbproc)) != NO_MORE_ROWS)
  84.         {
  85.             /* If DB-LIBRARY's row buffer is full, throw
  86.             * away the oldest row to allow the newest
  87.             * row to be read in.
  88.             */
  89.             if (row_code == BUF_FULL)
  90.                 dbclrbuf(dbproc, (DBINT) 1);
  91.         }
  92.  
  93.         /* Print out the column headers. */
  94.     
  95.         printf
  96.             (" %-20s %10s %25s %15s \n",
  97.             "    NAME    ",
  98.             "TYPE",
  99.             "    DATE    ",
  100.             "ID");
  101.         printf
  102.             (" %20s %10s %25s %15s \n",
  103.             "--------------------",
  104.             "----",
  105.             "----------------------",
  106.             "----");
  107.  
  108.         /* Let the user view any row in the table. */
  109.     
  110.         printf("Type the number of the row you want to see.\n");
  111.         printf("The first row is number 1.\n");
  112.         printf("Asking for row 0 will terminate the program.\n");
  113.  
  114.         while (quitflag == 0)
  115.         {
  116.             printf("Row number: ");
  117.             gets(numstring);
  118.             rownum = atoi(numstring);
  119.  
  120.         if (rownum == 0)
  121.             quitflag = 1;
  122.         else
  123.         {
  124.             /* Print out the requested row. */
  125.             if (dbgetrow(dbproc, rownum) == NO_MORE_ROWS)
  126.                 printf
  127.                 ("That row is not in the table.\n");
  128.             else
  129.             {
  130.                 /* Copy variable-length character data
  131.                 * (colname).
  132.                     */
  133.  
  134.                 strncpy
  135.                 (name, (DBCHAR *)dbdata(dbproc, 1),
  136.                 (len = dbdatlen(dbproc, 1)));
  137.  
  138.                 /* String needs terminating null. */
  139.  
  140.                 name[len] = '\0';
  141.  
  142.                 /* Copy fixed-length character data. */
  143.  
  144.                 strncpy
  145.                 (type, (DBCHAR *)dbdata(dbproc, 2),
  146.                 (len = dbdatlen(dbproc, 2)));
  147.                 type[len] = '\0';
  148.  
  149.                 /* Copy integer data. */
  150.                 id = *((DBINT *)dbdata(dbproc, 3));
  151.  
  152.                 /* Convert datetime data to a printable
  153.                 * string.
  154.                 */
  155.  
  156.                 dbconvert(dbproc, SQLDATETIME, (dbdata(dbproc, 4)),
  157.                 (DBINT)-1, SQLCHAR, datebuf, (DBINT)-1);
  158.                 printf
  159.                 ("%20s %10s %25s %15ld \n",
  160.                 name, type, datebuf, id);
  161.                 }
  162.             }
  163.         }
  164.     }
  165.  
  166.     dbexit();
  167.     exit(STDEXIT);
  168. }
  169.  
  170. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  171. DBPROCESS    *dbproc;
  172. int    severity;
  173. int    dberr;
  174. int    oserr;
  175. char    *dberrstr;
  176. char    *oserrstr;
  177. {
  178.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  179.         return(INT_EXIT);
  180.     else
  181.         {
  182.         printf("DB<MS>-LIBRARY error:\n\t%s\n", dberrstr);
  183.         if (oserr != DBNOERR)
  184.             printf("Operating-system error:\n\t%s\n", oserrstr);
  185.         return(INT_CANCEL);
  186.     }
  187. }
  188.  
  189. int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
  190. DBPROCESS    *dbproc;
  191. DBINT    msgno;
  192. int    msgstate;
  193. int    severity;
  194. char    *msgtext;
  195. {
  196.     printf
  197.     ("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  198.     msgno, msgstate, severity, msgtext);
  199.     return(0);
  200. }
  201.  
  202.