home *** CD-ROM | disk | FTP | other *** search
- #include "stdio.h"
- #include "sql.h"
- #include "math.h"
- #include "string.h"
-
- struct item /* data for sample table */
- {
- char desc[26];
- char price[6];
- }
- tabdata[]=
- {
- "CINNAMON", "1.00",
- "SAGE", ".95",
- "BASIL", ".89",
- "OREGANO",".89",
- "SAFFRON","3.00",
- "GINSENG","4.59" "", ""
- };
- static char note[]="sample.txt";
- static char dbnam[] = "xomed"; /* database name */
- static char ditem [] = /* DROP TABLE command */
- "DROP TABLE ITEM";
- static char crtitem [] = /* CREATE TABLE command */
- "CREATE TABLE ITEM (ITEM_NO NUMBER NOT NULL,DESCRIPTION CHAR(25), \
- PRICE DECIMAL (5,2), NOTES LONG VARCHAR)";
- static char insitem[] = /* INSERT command */
- "INSERT INTO ITEM VALUES (:1,:2,:3,:4)";
- static char selitem [] = /* SELECT command */
- "SELECT * FROM ITEM";
- static char updprice[] = /* UPDATE command */
- "UPDATE ITEM SET PRICE = :1 WHERE CURRENT OF C1";
- SQLTCUR cur1; /* SQLBase first cursor number */
- SQLTCUR cur2; /* SQLBase second cursor number */
- SQLTRCD rcd1; /* SQLBase return code */
- SQLTRCD rcd2; /* SQLBase return code */
- SQLTRCD rcd; /* SQLBase return code */
- char notebuf[200]; /* Buffer for long note */
- unsigned int itemno; /* Item number */
- char pricebuf[25]; /* Price area */
- char itembuf[26]; /* Item area */
-
- main()
- {
-
- /* CONNECT CUR1 TO THE DATABASE */
- if (rcd=sqlcnc(&cur1, dbnam,0))
- cncfail(rcd,"CONNECT");
-
- /* CONNECT CUR2 TO THE DATABASE */
- if (rcd=sqlcnc(&cur2, dbnam,0))
- cncfail(rcd,"CONNECT 2");
-
- /* DROP THE ITEM TABLE */
- sqlcex(cur1,ditem,0);
-
- /* CREATE THE ITEM TABLE */
- if (sqlcex(cur1, crtitem, 0))
- failure(cur1,"CREATE TABLE error");
-
- /* COMMIT */
- if (sqlcmt(cur1))
- failure(cur1,"ON CREATE COMMIT");
-
- itemins(); /* Insert the ITEM data */
-
- if (sqlcmt(cur1)) /* commit */
- failure(cur1,"ON INSERT COMMIT");
-
- priceupd (); /* Select and update prices */
- discnct(); /* Disconnect cursor */
- }
-
- itemins()
- {
- FILE *fp;
- struct item *datap; /* pointer to input data */
- int maxitem = 50; /* highest item number */
-
- /* Open the file for the getting the long text */
-
- /* Compile insert statement */
- if (sqlcom(cur1, insitem, 0))
- failure(cur1,"COMPILE ERROR");
-
- if ((fp = fopen(note, "r")) == NULL)
- failure(cur1,"OPEN FILE ERROR");
-
- /* Bind three columns to the area that contains data */
- if (sqlbnn(cur1,1,(SQLTDAP) &maxitem,sizeof(SQLPUIN),0,SQLPUIN))
- failure(cur1,"SQLBNN ERROR ");
- if (sqlbnn(cur1, 2, itembuf, 0, 0, SQLPSTR))
- failure(cur1,"SQLBNN ERROR ");
- if (sqlbnn(cur1,3,pricebuf,0,0,SQLPSTR))
- failure(cur1,"SQLBNN ERROR ");
-
- /* Bind the long column */
- if (sqlbln(cur1, 4))
- failure(cur1,"ERROR ON SQLBLN");
-
- datap = &tabdata[0]; /* Point to data */
- while (*(datap->desc) != '\0') /* Insert rows */
- {
- maxitem++; /* Get new item number */
- strcpy(itembuf, datap->desc); /* Copy description */
- strcpy(pricebuf,datap->price); /* Copy price */
- wrtlong(fp); /* Write the long field */
- if (sqlexe (cur1)) /* Execute the insert */
- failure(cur1,"ERROR ON EXECUTE");
- datap++; /* Next item to insert */
- }
-
- fclose (fp);
- printf ("DATA INSERTED \n");
- }
-
- /* The routine fetches each row, including long data,updates the price by 1 */
-
- priceupd ()
- {
- SQLTDAL len; /* Length of data read */
- SQLTRCD rcd; /* Fetch return code */
- char line[80]; /* Output buffer */
- char newprice[10]; /* Length of data read */
- double value;
- char* result;
- char ret_code = '\n';
-
- if (sqlscn(cur1, "C1", 2)) /* Name cursor C1 */
- failure(cur1,"SET CURSOR NAME");
- if (sqlcom(cur1, selitem, 0)) /* Compile select */
- failure(cur1,"SELECT COMPILE");
- if (sqlcom(cur2, updprice, 0)) /* Compile update */
- failure(cur2,"COMPILE ERROR");
-
- /* Bind price buffer for update statement */
-
- if (sqlbnn(cur2,1,(SQLTDAP)&value,sizeof(value),0,SQLPDOU))
- failure(cur2,"SQLBNN ERROR ");
-
- /* Set buffers for the character columns. Not necessary for last column,
- which is a long */
-
- if (sqlssb(cur1, 1, SQLPUIN, (char*) &itemno, sizeof(itemno),0,SQLNPTR,
- SQLNPTR))
- failure(cur1,"ERROR on SET SELECT BUFFER");
- if (sqlssb(cur1, 2,SQLPSTR, itembuf, sizeof (itembuf),0,SQLNPTR, SQLNPTR))
- failure(cur1,"ERROR on SET SELECT BUFFER");
- if (sqlssb(cur1, 3,SQLPSTR, pricebuf, sizeof (pricebuf),0,SQLNPTR, SQLNPTR))
- failure(cur1,"ERROR on SET SELECT BUFFER");
- if (sqlexe(cur1))
- failure(cur1,"Execute error");
- while (!(rcd=sqlfet(cur1))) /* Fetch one at a time */
- {
- sprintf (line, "%d %s %s\n", itemno, itembuf,pricebuf);
- printf ("%s\n", line);/* display data on the screen
-
- /* Read the long column and display */
-
- for (; ;)
- {
- memset (line, '\0', 80);
- if (rcd=sqlrlo(cur1, 4, line, sizeof(line) - 1, &len))
- failure(cur1,"Error on read long");
- else
- printf("%s\n", line); /* Display the data */
-
- if (len == 0) /* No more data for that row */
- {
- if (rcd=sqlelo(cur1)) /* End long */
- failure(cur1,"Error on end long");
- else
- break; /* Done processing long */
- }
- }
-
- /* Update the price according to user input */
-
- for (; ;)
- {
- printf ("Enter new price for %s; or return if no price change ",itembuf);
- /* Get user input */
- result=fgets (newprice, sizeof(newprice), stdin);
- if (*newprice == ret_code)
- {
- printf("No change in price \n");
- break;
- }
- else
- {
- value=atof(newprice);
- printf("price=%s \n",newprice);
- }
- if (rcd=sqlexe(cur2))
- failure(cur2,"update execute error");
- break;
- }
- }
- if (rcd != 1) /* If not end of fetch */
- failure(cur1,"Error on Fetch");
- if (sqlcmt (cur2)) /* Commit */
- failure(cur2,"ON UPDATE COMMIT");
- }
-
- /* This routine writes the long column using data from sample.txt */
-
- wrtlong (fp)
- FILE *fp;
- {
- int count;
-
- /* Open the file sample.txt which contains the long data. Each record
- entry for this program is marked with a line containing a backslash.
- When this is reached, end the long operation, move the other long
- data into its data area, and execute the insert. */
-
- for (; ;)
- {
- memset (notebuf,'\0',200);
- if ((fgets(notebuf,200,fp)) == NULL) /* Read file */
- break; /* End of file */
- if (notebuf[0] == '\\')
- break; /* No more row data */
- count = strlen(notebuf); /* How many bytes to write */
- if (sqlwlo(cur1,notebuf,count)) /* Write LONG */
- failure(cur1,"Error on WRITE LONG\n");
- }
- if (sqlelo(cur1)) /* End long operation */
- failure(cur1,"Error on End long");
- }
-
- failure(cur,p)
- SQLTCUR cur;
- char *p; /* Message string */
- {
- SQLTEPO epo; /* Error position */
- char errmsg [SQLMERR]; /* Error text */
-
- if (cur1 || cur2) /* Cursor 1 connected */
- {
- sqlrcd(cur,&rcd);
- sqlepo(cur,&epo); /* get error position */
- sqlerr(rcd,errmsg); /* Get error text */
- }
- printf("rcd=%d \n",rcd);
- printf("string in program=%s \n",p);
- printf("errmsg=%s \n",errmsg);
- printf("epo=%d \n",epo);
- discnct (); /* Disconnect cursors*/
- exit(1);
- }
- cncfail(ret,p)
- SQLTRCD ret;
- char *p; /* Message string */
- {
- SQLTEPO epo; /* Error position */
- char errmsg [SQLMERR]; /* Error text */
-
- sqlerr(rcd,errmsg); /* Get error text */
- printf("rcd=%d \n",rcd);
- printf("string in program=%s \n",p);
- printf("errmsg=%s \n",errmsg);
- exit(1);
- }
-
- discnct ()
- {
- if (cur1)
- sqldis(cur1);
- if (cur2)
- sqldis(cur2);
- }
-