#include <stdio.h>
#include <string.h>
exec sql include sqlca;
//From the book
#define ERRLEN 512
void print_dberror()
{
int errlength = ERRLEN;
int errsize;
extern sqlglm();
char errbuf[ERRLEN+1];
sqlglm(errbuf, & errlength, &errsize);
errbuf[errsize] = '\0';
printf( "%s\n" , errbuf );
}
int main()
{
exec sql begin declare section;
typedef enum { CHECKING=1 , SAVINGS , CREDIT_CARD , BUSINESS } ACCOUNT_TYPE;
typedef enum { DISALLOW , ALLOW } AGREEMENT; //determines access privelages on multi-user accounts
typedef struct {
int id ;
char name[50] ;
char *date_created ;
char *date_closed ;
float interest_rate ;
int type ; //because PROC is apparently too stupid to realize that enums are ints
float balance ;
int mutual_agreement ; //same as above
int contact_information_id ;
} ACCOUNT;
ACCOUNT accounts [] = {
//id account name opened closed rate type balance agreement ci_id
{ 1 , "Josh" , "15-JAN-05 12.00.00.000000" , NULL , 0.03 , SAVINGS , 20000 , DISALLOW , 1 },
{ 2 , "Josh" , "12-FEB-05 12.00.00.000000" , NULL , 0.20, CREDIT_CARD , 20000 , DISALLOW , 1 },
{ 3 , "Sally" , "15-MAR-07 12.00.00.000000" , NULL , 0.10, SAVINGS , 15000 , DISALLOW , 2 }, //Winston sisters each have their own contact information
{ 4 , "Julie" , "16-APR-09 12.00.00.000000" , NULL , 0.75 , SAVINGS , 14000 , DISALLOW , 4 }, //Winston sisters each have their own contact information
{ 5 , "Gen" , "29-MAY-05 12.00.00.000000" , NULL , 0.34 , CHECKING , 10000 , DISALLOW , 5 },
{ 6 , "Kanako" , "19-APR-07 12.00.00.000000" , NULL , 0.55 , CHECKING , 5000, DISALLOW , 6 },
{ 7 , "Kanako" , "11-SEP-08 12.00.00.000000" , NULL , 0.24 , CREDIT_CARD , 20000 , DISALLOW , 6 },
{ 8 , "Winston's Western Wear" , "19-FEB-08 12.00.00.000000" , NULL , 0.75 , BUSINESS , 10000 , ALLOW , 2 }, //Winston sisters' business is run out of Sally's house, so same CI
{ 9 , "Hoang" , "25-SEP-06 12.00.00.000000" , NULL , 0.56 , SAVINGS , 20000 , DISALLOW , 9 },
{ 10 , "Sanford Inc." , "11-OCT-07 12.00.00.000000" , NULL , 0.25 , BUSINESS , 10000 , DISALLOW , 4 }, //this business has it's own ci
{ 11 , "Anh" , "26-NOV-08 12.00.00.000000" , NULL , 0.55 , CHECKING , 10000 , DISALLOW , 11 }, //Anh has 3 different types of accounts
{ 12 , "Anh" , "17-DEC-07 12.00.00.000000" , NULL , 0.35 , SAVINGS , 10000 , DISALLOW , 11 },
{ 13 , "Anh" , "17-DEC-07 12.00.00.000000" , NULL , 0.35 , CREDIT_CARD , 10000 , DISALLOW , 11 }
};
char *user_name="username" , *user_pwd="secret";
int i = 0 , size_of_accounts = sizeof(accounts)/sizeof(ACCOUNT);
exec sql end declare section;
exec sql whenever sqlerror goto report_error;
exec sql connect :user_name
identified by :user_pwd;
for( i = 0; i < size_of_accounts; i++)
{
exec sql insert into accounts ( id, name, date_created , date_closed, interest_rate, type, balance, mutual_agreement, contact_information_id)
values ( :accounts[i].id,
:accounts[i].name,
:accounts[i].date_created,
:accounts[i].date_closed,
:accounts[i].interest_rate,
:accounts[i].type,
:accounts[i].balance,
:accounts[i].mutual_agreement,
:accounts[i].contact_information_id
);
exec sql commit work;
}
exec sql commit release;
return 0;
report_error:
printf( "ERROR i=%d\n" , i );
print_dberror();
return 1;
}