Example of a C Program






Example of a C Program

In this chapter, we used a pseudo programming language for all the examples. In this section, we give two small examples of programs that have been written in the C programming language and, therefore, contain all the C details.

Figure. Develop a C program that creates the TEAMS table.

#include <stdio.h>

EXEC SQL BEGIN DECLARE SECTION;
EXEC SQL END DECLARE SECTION;

EXEC SQL INCLUDE SQLCA;

main()
   {
   EXEC SQL CONNECT SPORTDB;
   if (sqlca.sqlcode = 0)
      {
      EXEC SQL CREATE TABLE TEAMS ( ... );
      printf("The TEAMS table has been created. \n");
      EXEC SQL COMMIT WORK;
      }

   exit(0);
   }

Here, you can see clearly the details that we omitted in all our previous examples, such as the statements BEGIN and END DECLARE SECTION, INCLUDE, and CONNECT.

Figure. Develop a C program that adds a row to the TEAMS table.

#include <stdio.h>

EXEC SQL BEGIN DECLARE SECTION;
     int     tno;
     int     pno;
     VARCHAR division[6];
EXEC SQL END DECLARE SECTION;

EXEC SQL INCLUDE SQLCA;

main()
   {
   EXEC SQL CONNECT SPORTDB;

   if (sqlca.sqlcode = 0)
      {
      printf("Enter a team number: ");
      scanf("%d",&tno);
      printf("Enter the number of the captain: ");
      scanf("%d",&pno);
      printf("Enter the division: ");
      scanf("%s",division.arr);
      division.len = strlen(division.arr);

      EXEC SQL INSERT INTO TEAMS
                      (TEAMNO, PLAYERNO, DIVISION)
               VALUES (:tno, :pno, :division);
      EXEC SQL COMMIT WORK;

      printf("The team has been added. \n");
      }
   exit(0);
   }



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows