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