home *** CD-ROM | disk | FTP | other *** search
- #include "sql.h"
- #include "errsql.h"
- #include "stdio.h"
-
- /*----------------------------------------------------------------*/
- /* */
- /* Example writing long data */
- /* */
- /*----------------------------------------------------------------*/
-
- SQLTCUR cur; /* SQLBASE cursor number */
- SQLTRCD rcd; /* error number */
- char errmsg[SQLMERR]; /* error msg text buffer */
-
- main()
- {
- FILE* fp; /* file pointer */
- SQLTROW rows; /* number of rows */
- int count; /* saying number to use */
- char buf[80]; /* long varchar write buf */
- static char create [] = /* CREATE TABLE statement */
- "CREATE TABLE SAYINGS (SAY_NO NUMBER NOT NULL, SAY_TEXT LONG VARCHAR)";
- static char insert [] = /* INSERT statement */
- "INSERT INTO SAYINGS VALUES (:1, :2)";
-
- /* CONNECT TO THE DATABASE */
-
- if (rcd = sqlcnc(&cur, "DEMO", 0))
- {
- sqlerr(rcd, errmsg); /* get error message text */
- printf("%s \n",errmsg);
- exit(1);
- }
-
- /* CREATE SAYINGS TABLE */
-
- if (rcd = sqlcex(cur, create, 0))
- {
- if (rcd != EXEETVS) /* not error if tbl exist */
- failure("CREATE SAYINGS TABLE");
- }
- else
- printf("SAYINGS TABLE CREATED \n");
-
- /* COMPUTE SAYINGS NUMBER */
-
- if (sqlgnr(cur, "SAYINGS", 0, &rows))
- failure("GET NUMBER OF ROWS");
-
- count = rows + 1; /* compute sayings number */
-
- /* COMPILE INSERT STATEMENT */
-
- if (sqlcom(cur, insert, 0))
- failure("COMPILE OF INSERT");
-
- /* BIND BY NUMBER */
-
- if (sqlbnn(cur, 1, (SQLTDAP) &count, sizeof(count), 0, SQLPUIN))
- failure("BINDING COUNT");
-
- if (sqlbln(cur, 2))
- failure("BINDING LONG");
-
- /*
- WRITE LONG DATA
- */
-
- if (!(fp = fopen("SAYINGS.1", "r"))) /* open saying text file */
- failure("FILE OPEN");
-
- while (fgets(buf,sizeof(buf),fp)) /* read the saying text */
- if (sqlwlo(cur,buf,0))
- failure("WRITE LONG");
-
- if (fclose(fp))
- failure("FILE CLOSE");
-
- /* END LONG OPERATION */
-
- if (sqlelo(cur))
- failure("ENDING LONG OPERATION");
-
- /* EXECUTE INSERT STATEMENT */
-
- if (sqlexe(cur))
- failure("EXECUTE");
- else
- printf("SAYING NUMBER %d SUCCESSFULLY INSERTED\n",count);
-
- /* DISCONNECT FROM THE DATABASE */
-
- if (sqldis(cur))
- failure("DISCONNECT");
- }
- failure(ep)
- char* ep; /* -> failure msg string */
- {
- SQLTEPO epo; /* error position */
-
- printf("Failure on %s \n", ep);
- sqlrcd(cur, &rcd); /* get the error */
- sqlepo(cur, &epo); /* get error position */
- sqlerr(rcd, errmsg); /* get error message text */
- sqldis(cur);
- printf("%s (error: %u, position: %u) \n",errmsg,rcd,epo);
- exit(1);
- }
-