home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a063 / 9.img / SAMPLE / DBLIB / EXAMPLE6.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-02  |  5.4 KB  |  204 lines

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