home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 6 / 06.iso / a / a600 / 5.ddi / EX13.C / EX13.C
Encoding:
C/C++ Source or Header  |  1990-03-28  |  2.7 KB  |  109 lines

  1. #include "sql.h"
  2. #include "errsql.h"
  3. #include "stdio.h"
  4.  
  5. /*----------------------------------------------------------------*/
  6. /*                                  */
  7. /*  Example writing long data                      */
  8. /*                                  */
  9. /*----------------------------------------------------------------*/
  10.  
  11.       SQLTCUR   cur;        /* SQLBASE cursor number  */
  12.       SQLTRCD   rcd;        /* error number       */
  13.       char        errmsg[SQLMERR];    /* error msg text buffer  */
  14.  
  15.       main()
  16. {
  17.       FILE*     fp;         /* file pointer       */
  18.       SQLTROW   rows;        /* number of rows      */
  19.       int        count;        /* saying number to use   */
  20.       char        buf[80];        /* long varchar write buf */
  21. static      char        create [] =     /* CREATE TABLE statement */
  22. "CREATE TABLE SAYINGS (SAY_NO NUMBER NOT NULL, SAY_TEXT LONG VARCHAR)";
  23. static      char        insert [] =     /* INSERT statement      */
  24. "INSERT INTO SAYINGS VALUES (:1, :2)";
  25.  
  26.   /* CONNECT TO THE DATABASE */
  27.  
  28.   if (rcd = sqlcnc(&cur, "DEMO", 0))
  29.   {
  30.     sqlerr(rcd, errmsg);        /* get error message text */
  31.     printf("%s \n",errmsg);
  32.     exit(1);
  33.   }
  34.  
  35.   /* CREATE SAYINGS TABLE */
  36.  
  37.   if (rcd = sqlcex(cur, create, 0))
  38.   {
  39.     if (rcd != EXEETVS)         /* not error if tbl exist */
  40.       failure("CREATE SAYINGS TABLE");
  41.   }
  42.   else
  43.     printf("SAYINGS TABLE CREATED \n");
  44.  
  45.   /* COMPUTE SAYINGS NUMBER */
  46.  
  47.   if (sqlgnr(cur, "SAYINGS", 0, &rows))
  48.     failure("GET NUMBER OF ROWS");
  49.  
  50.   count = rows + 1;            /* compute sayings number */
  51.  
  52.   /* COMPILE INSERT STATEMENT */
  53.  
  54.   if (sqlcom(cur, insert, 0))
  55.     failure("COMPILE OF INSERT");
  56.  
  57.   /* BIND BY NUMBER */
  58.  
  59.   if (sqlbnn(cur, 1, (SQLTDAP) &count, sizeof(count), 0, SQLPUIN))
  60.     failure("BINDING COUNT");
  61.  
  62.   if (sqlbln(cur, 2))
  63.     failure("BINDING LONG");
  64.  
  65.   /*
  66.     WRITE LONG DATA
  67.   */
  68.  
  69.   if (!(fp = fopen("SAYINGS.1", "r")))    /* open saying text file  */
  70.     failure("FILE OPEN");
  71.  
  72.   while (fgets(buf,sizeof(buf),fp))    /* read the saying text   */
  73.     if (sqlwlo(cur,buf,0))
  74.       failure("WRITE LONG");
  75.  
  76.   if (fclose(fp))
  77.     failure("FILE CLOSE");
  78.  
  79.   /* END LONG OPERATION */
  80.  
  81.   if (sqlelo(cur))
  82.     failure("ENDING LONG OPERATION");
  83.  
  84.   /* EXECUTE INSERT STATEMENT */
  85.  
  86.   if (sqlexe(cur))
  87.     failure("EXECUTE");
  88.   else
  89.     printf("SAYING NUMBER %d SUCCESSFULLY INSERTED\n",count);
  90.  
  91.   /* DISCONNECT FROM THE DATABASE */
  92.  
  93.   if (sqldis(cur))
  94.     failure("DISCONNECT");
  95. }
  96.       failure(ep)
  97.       char*     ep;         /* -> failure msg string  */
  98. {
  99.       SQLTEPO   epo;        /* error position      */
  100.  
  101.   printf("Failure on %s \n", ep);
  102.   sqlrcd(cur, &rcd);            /* get the error      */
  103.   sqlepo(cur, &epo);            /* get error position      */
  104.   sqlerr(rcd, errmsg);            /* get error message text */
  105.   sqldis(cur);
  106.   printf("%s (error: %u, position: %u) \n",errmsg,rcd,epo);
  107.   exit(1);
  108. }
  109.