​A selection of topics on IT and its application to finance. 
​Send me your comments, questions ​or suggestions by clicking
h​ere


elmama
A simple Embedded C/SQL program showing the use of a cursor 



#include <stdio.h>
#include <stdlib.h>

EXEC SQL INCLUDE "sqlca.h";

void sql_error();
void db_connect();
void get_tables();
void print_tables();



EXEC SQL BEGIN DECLARE SECTION;
    char table_name[31];
EXEC SQL END DECLARE SECTION;

EXEC SQL VAR table_name IS STRING;

int nr_tables = 0;

main()
{
    EXEC SQL WHENEVER SQLERROR do sql_error();

    db_connect();

    get_tables();

    print_tables();
}

void get_tables()
{
    EXEC SQL DECLARE c1 cursor for SELECT table_name
    FROM user_tables;
}


void print_tables()
{
    int i = 0;
    EXEC SQL OPEN c1;

    EXEC SQL WHENEVER NOT FOUND DO break;
    while(1)
    {
       EXEC SQL FETCH c1 into :table_name;
       fprintf(stdout,"%d %s\n",i++,table_name);
    }
}

void sql_error()
{

    fprintf(stdout,"Error number = %d\n",sqlca.sqlcode);
    fprintf(stdout,"Error message = %s\n",sqlca.sqlerrm.sqlerrmc);
    exit(-1);
}

void db_connect()
{
    EXEC SQL BEGIN DECLARE SECTION;
    char *username = "scott";
    char *password = "tiger";
    EXEC SQL END DECLARE SECTION;


    EXEC SQL CONNECT :username IDENTIFIED BY :password;
}