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

  1. /*    example6.c */
  2. /*
  3. ** This example illustrates opening a data file, inserting data
  4. ** from the file into a newly created table containing several
  5. ** SQL Server datatypes, and updating the table using browse mode
  6. ** techniques.
  7. */
  8.  
  9. #define DBMSOS2
  10. #include <stdio.h>
  11. #include <sqlfront.h>
  12. #include <sqldb.h>
  13.  
  14. #define STDEXIT 0
  15. #define BUFLEN 2048
  16.  
  17. /* Forward declarations of the error-handling and message-handing
  18. functions. */
  19. int    err_handler();
  20. int    msg_handler();
  21.  
  22. main()
  23. {
  24.     LOGINREC    *login;
  25.     DBPROCESS    *q_dbproc;    /* This DBPROCESS is used to
  26.     * query the database.
  27.     */
  28.     DBPROCESS    *u_dbproc;    /* This DBPROCESS is used to
  29.     * simultaneously update the database.
  30.     */
  31.     char    *qualptr;    /* This points to the WHERE clause
  32.     * appropriate for updating q_dbproc's
  33.     * current data row.
  34.     */
  35.     RETCODE    return_code;
  36.     DBTINYINT    age;
  37.     static char    cmdbuf[BUFLEN];
  38.     FILE    *infile;
  39.  
  40.     /* Install the user-supplied error-handling and message-handling
  41.     * functions. They are defined at the bottom of this source file.
  42.     */
  43.     dberrhandle(err_handler);
  44.     dbmsghandle(msg_handler);
  45.  
  46.     /* Allocate and initialize the LOGINREC structure to be used
  47.     * to open a connection to SQL Server.
  48.     */
  49.  
  50.     login = dblogin();
  51.  
  52.         DBSETLUSER(login, "user");
  53.     DBSETLPWD(login, "my_passwd");
  54.     DBSETLAPP(login, "example6");
  55.  
  56.     q_dbproc = dbopen(login, "my_server");
  57.     u_dbproc = dbopen(login, "my_server");
  58.  
  59.     printf("Creating the 'alltypes' table.\n");
  60.  
  61.     /* Create a table that contains several SQL Server datatypes. */
  62.     dbcmd(q_dbproc,"create table alltypes ");
  63.     dbcmd(q_dbproc,"(age tinyint,");
  64.     dbcmd(q_dbproc,"userid smallint,");
  65.     dbcmd(q_dbproc,"royalty int,");
  66.     dbcmd(q_dbproc,"name char(25),");
  67.     dbcmd(q_dbproc,"title_id varbinary(20),");
  68.     dbcmd(q_dbproc,"us_citizen bit,");
  69.     dbcmd(q_dbproc,"account float,");
  70.     dbcmd(q_dbproc,"title varchar(20),");
  71.     dbcmd(q_dbproc,"manager char(25),");
  72.     dbcmd(q_dbproc,"timestamp)");
  73.  
  74.     dbcmd(q_dbproc, "create unique index index1 on alltypes(userid)");
  75.  
  76.     dbsqlexec(q_dbproc);
  77.     while (dbresults(q_dbproc) != NO_MORE_RESULTS)
  78.     continue;
  79.  
  80.     /* Insert rows of data into the newly created table "alltypes".
  81.     * We will read in the contents of the file and form an
  82.     * INSERT statement.
  83.     */
  84.  
  85.     if ((infile=fopen("datafile","r")) == NULL)
  86.     {
  87.         printf("Unable to open file 'datafile'.\n");
  88.         exit(STDEXIT);
  89.     }
  90.  
  91.     printf("Inserting rows into the 'alltypes' table...\n");
  92.  
  93.     while ((fgets(cmdbuf,BUFLEN,infile)) != NULL)
  94.     {
  95.         dbfcmd(q_dbproc,"insert into alltypes \n");
  96.         dbfcmd(q_dbproc,"values(%s, null) \n",cmdbuf);
  97.     }
  98.  
  99.     dbsqlexec(q_dbproc);
  100.  
  101.     /* Process the results of each of the INSERT statements. */
  102.  
  103.     while ((return_code = dbresults(q_dbproc)) != NO_MORE_RESULTS)
  104.     {
  105.         if (return_code == FAIL)
  106.             printf("One of the insert statements failed.\n");
  107.     }
  108.  
  109.     /* Using DB-LIBRARY's browse mode facilities, we'll increment
  110.     * the age of every person in the table.
  111.     */
  112.  
  113.     printf("Updating rows in the 'alltypes' table...\n");
  114.  
  115.     dbcmd(q_dbproc,"select * from alltypes for browse");
  116.     dbsqlexec(q_dbproc);
  117.  
  118.     while ((return_code = dbresults(q_dbproc)) != NO_MORE_RESULTS)
  119.     {
  120.         if (return_code == SUCCEED)
  121.         {
  122.             while (dbnextrow(q_dbproc) != NO_MORE_ROWS)
  123.             {
  124.                 age = *((DBTINYINT *)(dbdata(q_dbproc, 1)));
  125.                 qualptr = dbqual(q_dbproc, -1, "alltypes");
  126.                 dbcmd(u_dbproc, "update alltypes");
  127.                 dbfcmd
  128.                 (u_dbproc, " set age = %d %s", age+1, qualptr);
  129.                 dbsqlexec(u_dbproc);
  130.                 dbresults(u_dbproc);
  131.                 dbfreequal(qualptr);
  132.             }
  133.         }
  134.     }
  135.  
  136.     /* Now, we'll look at the updated contents of the table to
  137.     * verify that the ages were properly incremented.
  138.     */
  139.     printf("Selecting rows from the 'alltypes' table:\n");
  140.     dbcmd(q_dbproc, "select * from alltypes");
  141.     dbsqlexec(q_dbproc);
  142.     dbresults(q_dbproc);
  143.     dbprrow(q_dbproc);
  144.  
  145.     dbexit();
  146.     exit(STDEXIT);
  147. }
  148.  
  149. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  150. DBPROCESS    *dbproc;
  151. int    severity;
  152. int    dberr;
  153. int    oserr;
  154. char    *dberrstr;
  155. char    *oserrstr;
  156. {
  157.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  158.         return(INT_EXIT);
  159.     else
  160.     {
  161.         printf("DB-LIBRARY error:\n\t%s\n", dberrstr);
  162.  
  163.         if (oserr != DBNOERR)
  164.             printf("Operating-system error:\n\t%s\n", oserrstr);
  165.         return(INT_CANCEL);
  166.     }
  167. }
  168.  
  169. int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
  170. DBPROCESS    *dbproc;
  171. DBINT    msgno;
  172. int    msgstate;
  173. int    severity;
  174. char    *msgtext;
  175. {
  176.     printf
  177.     ("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  178.     msgno, msgstate, severity, msgtext);
  179.     return(0);
  180. }
  181.  
  182.  
  183.