Contents


Creating a Genero ESQL/C Program

See also: Host variables, Supported data typesSQL statements, Dynamic SQL, Compiling programs


Overview of Genero ESQL/C

Genero ESQL/C (FESQLC) allows you to embed SQL statements in your C-language programs in order to communicate with a relational database.

The keywords "EXEC SQL" (preferred ANSI standard) or the $ symbol precede SQL statements and FESQLC pre-processor directives. The statements terminate with a semi-colon:
EXEC SQL CONNECT TO 'custdemo';
EXEC SQL DECLARE c1 CURSOR FOR SELECT * FROM orders;
Host variables can substitute for literal values in your programs. The host variable stores the values retrieved from a database table or serves as a parameter for SQL statements. Declare host variables in your program in a host variable declaration block. Precede the host variable with the symbol (:) whenever it is used in an embedded SQL statement:
EXEC SQL SELECT store_name INTO :p_store_name FROM customer;
You can create dynamic SQL statements that are constructed at runtime, based on some program conditions or the user's interaction.

FESQLC supports the common SQL data types, but some, such as DECIMAL, do not have corresponding C data types. FESQLC provides additional data types that you can use in your programs.

Functions and macros that you can use in your program, including specific functions to manipulate date, time, character string, and decimal values,  are a part of FESQLC.

Some FESQLC functions require data type constants as arguments. See Supported Data Types for a list of the data type constants associated with FESQLC data types and their numeric value.

Exception handling in FESQLC allows you to obtain information about the execution of an SQL statement and to handle program errors.

The FESQLC preprocessor directives allow you to:

The fesqlc tool compiles and links C programs that contain FESQLC source code files, creating an executable C program.

These topics are discussed in detail in the other pages of this documentation.


Prerequisites

Before you begin using Genero ESQL/C (FESQLC), make sure the following has been done:


Genero ESQL/C header files

Genero ESQL/C (FESQLC) automatically includes any header files that it requires in order to preprocess your program. See Preprocessor directives for information about including other types of files in your FESQLC programs.

Use the standard C #include directive to include C header files. The #include of C includes a file after FESQLC preprocessing.

Warning: 

For migration purposes, include statements for Informix ESQL/C header files are permitted, although they are not required and the FESQLC compiler will ignore them. However, if your program has such statements, you must use the EXEC SQL syntax. The following C syntax is not supported:

#include "sqlca.h"

FESQLC will consider this a normal C header file to be included. The statement must be replaced by:

EXEC SQL include sqlca;

Example program

01 #include <stdio.h>
02
03 EXEC SQL define NAME_LEN 20;  /* customer.store_name is a CHAR(20) */
04 EXEC SQL define ADDR_LEN 20;  /* customer.addr is a CHAR(20)       */
05
06 void errlog(void)             /* function to handle exceptions  */
07 {
08  fprintf(stderr, "Error occurred:\n");
09  fprintf(stderr, "  SQLSTATE = [%s]  SQLCODE = %d\n\n",
10                  SQLSTATE, SQLCODE);
11  EXEC SQL DISCONNECT ALL;
12  exit(1);
13 }
14
15 int main()
16 {
17  EXEC SQL BEGIN DECLARE SECTION;   /* defining host variables */
18    int  p_num;
19    varchar p_name[ NAME_LEN + 1 ];
20    int2 i_name;                    /* indicator variable      */
21    varchar p_addr[ ADDR_LEN + 1 ];
22    int2 i_addr;                    /* indicator variable      */
23    varchar p_addr2[ ADDR_LEN + 1 ];
24    int2 i_addr2;                   /* indicator variable      */
25    char p_state[3]; 
26  EXEC SQL END DECLARE SECTION;
27
28  EXEC SQL WHENEVER ERROR CALL errlog; /* set error handling */
29
30  printf( "Connecting...\n\n");
31  EXEC SQL CONNECT TO 'custdemo';     /* connect to database  */
32  /* use database cursor to retrieve data    */
33  EXEC SQL DECLARE c1 CURSOR FOR
34    SELECT store_num, store_name, addr, addr2
35      FROM customer
36      WHERE state = :p_state;
37
38  strcpy(p_state, "IL");
39  EXEC SQL OPEN c1;
40
41  for (;;)
42  {
43
44   EXEC SQL FETCH c1 INTO :p_num,
45                           :p_name INDICATOR :i_name,
46                           :p_addr INDICATOR :i_addr,
47                           :p_addr2 INDICATOR :i_addr2;
48   /* check SQLSTATE for end of data   */
49   if (strncmp(SQLSTATE, "02", 2) == 0) {
50        /* No more rows */
51        break;
52   }
53
54   printf("%6d %-20s\n      %s %s\n",
55            p_num,
56            i_name == 0 ? p_name : "<no name>",
57            i_addr == 0 ? p_addr : "<no address>",
58            i_addr2 == 0 ? p_addr2 : ""
59   );
60  }
61
62  EXEC SQL CLOSE c1;
63  EXEC SQL FREE c1;
64
65  printf("\nDisconnecting...\n\n");
66  EXEC SQL DISCONNECT CURRENT;
67
68  return 0;
69 }