home *** CD-ROM | disk | FTP | other *** search
/ WinWares 1 / WINWARES.ISO / database / gupta / ex19.c / ex19.c
Encoding:
C/C++ Source or Header  |  1993-12-11  |  7.6 KB  |  273 lines

  1. #include "stdio.h"
  2. #include "sql.h"
  3. #include "math.h"
  4. #include "string.h"
  5.  
  6.     struct item            /* data for sample table        */
  7.     {
  8.         char desc[26];
  9.         char price[6];
  10.     }
  11.     tabdata[]=
  12.     {
  13.         "CINNAMON", "1.00",
  14.         "SAGE", ".95",
  15.         "BASIL", ".89",
  16.         "OREGANO",".89",
  17.         "SAFFRON","3.00",
  18.         "GINSENG","4.59"    "", ""
  19.     };
  20. static    char    note[]="sample.txt";
  21. static    char    dbnam[] = "xomed";    /* database name            */
  22. static    char    ditem [] =        /* DROP TABLE command            */
  23. "DROP TABLE ITEM";
  24. static    char    crtitem [] =        /* CREATE TABLE command            */
  25. "CREATE TABLE ITEM (ITEM_NO NUMBER NOT NULL,DESCRIPTION CHAR(25), \
  26. PRICE DECIMAL (5,2), NOTES LONG VARCHAR)";
  27. static char insitem[] =            /* INSERT command            */
  28. "INSERT INTO ITEM VALUES (:1,:2,:3,:4)";
  29. static char selitem [] =        /* SELECT command            */
  30. "SELECT  * FROM ITEM";
  31. static char updprice[] =        /* UPDATE command            */
  32. "UPDATE ITEM SET PRICE = :1 WHERE CURRENT OF C1";
  33.     SQLTCUR cur1;            /* SQLBase first cursor number        */
  34.     SQLTCUR cur2;            /* SQLBase second cursor number        */
  35.     SQLTRCD rcd1;            /* SQLBase return code            */
  36.     SQLTRCD rcd2;            /* SQLBase return code            */
  37.     SQLTRCD rcd;            /* SQLBase return code            */
  38.     char notebuf[200];        /* Buffer for long note            */
  39.     unsigned int itemno;        /* Item number                */
  40.     char pricebuf[25];        /* Price area                */
  41.     char itembuf[26];        /* Item area                */
  42.  
  43.     main()
  44. {
  45.     
  46. /* CONNECT CUR1 TO THE DATABASE */
  47. if (rcd=sqlcnc(&cur1, dbnam,0))
  48.   cncfail(rcd,"CONNECT");
  49.  
  50. /* CONNECT CUR2 TO THE DATABASE */
  51. if (rcd=sqlcnc(&cur2, dbnam,0))
  52.   cncfail(rcd,"CONNECT 2");
  53.  
  54. /* DROP THE ITEM TABLE  */
  55. sqlcex(cur1,ditem,0);
  56.  
  57. /* CREATE THE ITEM TABLE */
  58. if (sqlcex(cur1, crtitem, 0))
  59.   failure(cur1,"CREATE TABLE error");
  60.  
  61. /* COMMIT */
  62. if (sqlcmt(cur1))        
  63.   failure(cur1,"ON CREATE COMMIT");
  64.  
  65. itemins();                /* Insert the ITEM data            */
  66.  
  67. if (sqlcmt(cur1))            /* commit                */
  68.   failure(cur1,"ON INSERT COMMIT");
  69.  
  70. priceupd ();                /* Select and update prices        */
  71. discnct();                /* Disconnect cursor            */
  72. }
  73.  
  74.       itemins()
  75. {
  76.       FILE *fp;
  77.       struct item *datap;        /* pointer to input data        */
  78.       int maxitem = 50;        /* highest item number            */
  79.  
  80. /* Open the file for the getting the long text */
  81.  
  82. /* Compile insert statement */
  83. if (sqlcom(cur1, insitem, 0))
  84.   failure(cur1,"COMPILE ERROR");
  85.  
  86. if ((fp = fopen(note, "r")) == NULL)
  87.   failure(cur1,"OPEN FILE ERROR");
  88.     
  89. /* Bind three columns to the area that contains data */
  90. if (sqlbnn(cur1,1,(SQLTDAP) &maxitem,sizeof(SQLPUIN),0,SQLPUIN))
  91.   failure(cur1,"SQLBNN ERROR ");
  92. if (sqlbnn(cur1, 2, itembuf, 0, 0, SQLPSTR))
  93.   failure(cur1,"SQLBNN ERROR ");
  94. if (sqlbnn(cur1,3,pricebuf,0,0,SQLPSTR))
  95.   failure(cur1,"SQLBNN ERROR ");
  96.  
  97. /* Bind the long column */
  98. if (sqlbln(cur1, 4)) 
  99.   failure(cur1,"ERROR ON SQLBLN");
  100.     
  101. datap = &tabdata[0];            /* Point to data            */
  102. while (*(datap->desc) != '\0')        /* Insert rows                */
  103. {
  104.     maxitem++;            /* Get new item number            */
  105.     strcpy(itembuf, datap->desc);    /* Copy description            */
  106.     strcpy(pricebuf,datap->price);    /* Copy price                */
  107.     wrtlong(fp);            /* Write the long field            */
  108.     if (sqlexe (cur1))        /* Execute the insert            */
  109.       failure(cur1,"ERROR ON EXECUTE");
  110.     datap++;            /* Next item to insert            */
  111. }
  112.  
  113. fclose (fp);
  114. printf ("DATA INSERTED \n");
  115. }
  116.  
  117. /* The routine fetches each row, including long data,updates the price by 1 */
  118.  
  119.        priceupd ()
  120. {
  121.        SQLTDAL len;            /* Length of data read            */
  122.        SQLTRCD rcd;            /* Fetch return code            */
  123.        char line[80];        /* Output buffer            */
  124.        char newprice[10];        /* Length of data read            */
  125.        double value;
  126.        char* result;
  127.        char  ret_code = '\n';
  128.        
  129. if (sqlscn(cur1, "C1", 2))        /* Name cursor C1            */
  130.   failure(cur1,"SET CURSOR NAME");
  131. if (sqlcom(cur1, selitem, 0))        /* Compile select            */
  132.   failure(cur1,"SELECT COMPILE");
  133. if (sqlcom(cur2, updprice, 0))        /* Compile update            */
  134.   failure(cur2,"COMPILE ERROR");
  135.  
  136. /* Bind price buffer for update statement */
  137.  
  138. if (sqlbnn(cur2,1,(SQLTDAP)&value,sizeof(value),0,SQLPDOU))
  139.   failure(cur2,"SQLBNN ERROR ");
  140.     
  141. /* Set buffers for the character columns. Not necessary for last column,
  142.     which is a long */
  143.  
  144. if (sqlssb(cur1, 1, SQLPUIN, (char*) &itemno, sizeof(itemno),0,SQLNPTR, 
  145.     SQLNPTR))
  146.   failure(cur1,"ERROR on SET SELECT BUFFER");
  147. if (sqlssb(cur1, 2,SQLPSTR, itembuf, sizeof (itembuf),0,SQLNPTR, SQLNPTR))
  148.   failure(cur1,"ERROR on SET SELECT BUFFER");
  149. if (sqlssb(cur1, 3,SQLPSTR, pricebuf, sizeof (pricebuf),0,SQLNPTR, SQLNPTR))
  150.   failure(cur1,"ERROR on SET SELECT BUFFER");
  151. if (sqlexe(cur1))
  152.   failure(cur1,"Execute error");
  153. while (!(rcd=sqlfet(cur1)))        /* Fetch one at a time            */
  154. {
  155.   sprintf (line, "%d  %s  %s\n", itemno, itembuf,pricebuf);
  156.   printf ("%s\n", line);/* display data on the screen
  157.  
  158. /* Read the long column and display */
  159.  
  160.   for (; ;)
  161.   {
  162.     memset (line, '\0', 80);
  163.     if (rcd=sqlrlo(cur1, 4, line, sizeof(line) - 1, &len))
  164.       failure(cur1,"Error on read long");
  165.     else
  166.       printf("%s\n", line);        /* Display the data            */
  167.  
  168.     if (len == 0)            /* No more data for that row        */
  169.     {
  170.       if (rcd=sqlelo(cur1))        /* End long                */
  171.         failure(cur1,"Error on end long");
  172.       else
  173.         break;            /* Done processing long            */
  174.     }
  175.   }
  176.  
  177. /* Update the price according to user input */
  178.  
  179.   for (; ;)
  180.   {
  181.     printf ("Enter new price for %s; or return if no price change ",itembuf);
  182.     /* Get user input */
  183.     result=fgets (newprice, sizeof(newprice), stdin); 
  184.     if (*newprice == ret_code)
  185.     {
  186.       printf("No change in price \n");
  187.       break;
  188.     }
  189.     else
  190.     {
  191.       value=atof(newprice);        
  192.       printf("price=%s \n",newprice);
  193.     }
  194.     if (rcd=sqlexe(cur2))
  195.       failure(cur2,"update execute error");
  196.     break;
  197.   }
  198. }
  199. if (rcd != 1)                /* If not end of fetch            */
  200.   failure(cur1,"Error on Fetch");
  201. if (sqlcmt (cur2))            /* Commit                */
  202.   failure(cur2,"ON UPDATE COMMIT");
  203. }
  204.  
  205. /* This routine writes the long column using data from sample.txt */
  206.  
  207.       wrtlong (fp)
  208.       FILE *fp;
  209. {
  210.       int count;
  211.  
  212. /* Open the file sample.txt which contains the long data.  Each record
  213.    entry for this program is marked with a line containing a backslash.
  214.    When this is reached, end the long operation, move the other long
  215.    data into its data area, and execute the insert. */
  216.  
  217. for (; ;)
  218. {
  219.   memset (notebuf,'\0',200);
  220.   if ((fgets(notebuf,200,fp)) == NULL)    /* Read file                */
  221.     break;                /* End of file                */
  222.   if (notebuf[0] == '\\')
  223.     break;                /* No more row data            */
  224.   count = strlen(notebuf);        /* How many bytes to write        */
  225.   if (sqlwlo(cur1,notebuf,count))    /* Write LONG                */
  226.     failure(cur1,"Error on WRITE LONG\n");
  227. }
  228. if (sqlelo(cur1))            /* End long operation            */
  229.   failure(cur1,"Error on End long");
  230. }
  231.  
  232.       failure(cur,p)
  233.       SQLTCUR cur;
  234.       char  *p;            /* Message string            */
  235. {
  236.       SQLTEPO    epo;        /* Error position            */
  237.       char        errmsg [SQLMERR];    /* Error text            */
  238.  
  239. if (cur1 || cur2)            /* Cursor 1 connected            */
  240. {
  241.   sqlrcd(cur,&rcd);
  242.   sqlepo(cur,&epo);            /* get error position            */
  243.   sqlerr(rcd,errmsg);            /* Get error text            */
  244. }
  245. printf("rcd=%d \n",rcd);
  246. printf("string in program=%s \n",p);
  247. printf("errmsg=%s \n",errmsg);
  248. printf("epo=%d \n",epo);
  249. discnct ();                /* Disconnect cursors*/
  250. exit(1);
  251. }
  252.       cncfail(ret,p)
  253.       SQLTRCD ret;
  254.       char  *p;            /* Message string            */
  255. {
  256.       SQLTEPO    epo;        /* Error position            */
  257.       char        errmsg [SQLMERR];    /* Error text            */
  258.  
  259. sqlerr(rcd,errmsg);            /* Get error text            */
  260. printf("rcd=%d \n",rcd);
  261. printf("string in program=%s \n",p);
  262. printf("errmsg=%s \n",errmsg);
  263. exit(1);
  264. }
  265.  
  266.       discnct ()
  267. {
  268. if (cur1)
  269. sqldis(cur1);
  270. if (cur2)
  271. sqldis(cur2);
  272. }
  273.