The following is an example that connects to the database defined as the default. All rows are then fetched from the table INFORMATION_SCHEMA.TABLES. A table named "Straße" is created, and dropped afterwards, to show the multi lingual support.
The example is stored in a file called simple.ec.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#define BUFLEN 129
#define lengthof(x) (sizeof(x)/sizeof(x[0]))
exec sql BEGIN DECLARE SECTION;
static char SQLSTATE[6];
exec sql END DECLARE SECTION;
main()
{
exec sql BEGIN DECLARE SECTION;
nvarchar schema[129], table[129];
varchar type[21];
exec sql END DECLARE SECTION;
char print_buffer[BUFLEN*6]; /* Multibyte character buffer */
setlocale(LC_ALL, "");
exec sql DECLARE MYCURSOR CURSOR FOR
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES;
/* Connect to default database */
exec sql CONNECT TO '' USER 'SYSADM' USING 'SYSADM';
exec sql CREATE TABLE Straße (col1 integer);
exec sql OPEN MYCURSOR;
while (1) {
exec sql FETCH MYCURSOR INTO :schema, :table, :type;
if (strcmp(SQLSTATE, "02000") == 0) break; /* No more rows */
wcstombs(print_buffer, schema, lengthof(print_buffer));
printf("%-25s\t", print_buffer);
wcstombs(print_buffer, table, lengthof(print_buffer));
printf("%-30s\t%-20s\n", print_buffer, type);
}
exec sql CLOSE MYCURSOR;
exec sql COMMIT;
exec sql DROP TABLE Straße CASCADE;
exec sql DISCONNECT ALL;
exit(0);
}
The program is preprocessed as follows:
esql −c simple.ec
The resulting file, "simple.c", is compiled using a C-compiler and then linked to the Mimer SQL shared library or DLL. Usually, as an aid to this step, an example makefile or a build procedure is available in the software distribution package.
When executing the resulting "simple" program, a list of the items selected will be displayed.
You can find a more complete example in distributed example program example.ec