//First, precompile it by // $ORACLE_HOME/bin/proc iname=oracle.pc code=cpp parse=none // //Then, compile it by // g++ -I$ORACLE_HOME/precomp/public/ oracle.c -L$ORACLE_HOME/lib/ -lclntsh -laio -lm // //At last, run it by // ./a.out #include #include #include #include void sql_error(char *msg) { printf("%s", msg); exit(1); } int main() { char temp_char[32]; //declear host variables EXEC SQL BEGIN DECLARE SECTION; VARCHAR username[32]; VARCHAR password[32]; VARCHAR str[33]; EXEC SQL END DECLARE SECTION; //Copy the username and password into the VARCHAR. strncpy((char *) username.arr, "YOUR_USERNAME", 32); username.len = strlen((char *) username.arr); strncpy((char *) password.arr, "YOUR_PASSWORD", 32); password.len = strlen((char *) password.arr); //Register sql_error() as the error handler. EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE ERROR: \n"); //Connect to ORACLE. Program will call sql_error() //if an error occurs when connecting to the default database. EXEC SQL CONNECT :username IDENTIFIED BY :password; printf("\nConnected to ORACLE as user: %s\n", username.arr); //create a table EXEC SQL CREATE TABLE hello (message varchar(32)); //insert data EXEC SQL INSERT INTO hello VALUES ('Hello World!'); //query the db EXEC SQL DECLARE tmp CURSOR FOR SELECT message FROM hello; EXEC SQL OPEN tmp; EXEC SQL WHENEVER NOT FOUND DO break; for (;;) { EXEC SQL FETCH tmp INTO :str; str.arr[str.len] = '\0'; printf("%s\n", str.arr); } EXEC SQL CLOSE tmp; // Disconnect from ORACLE EXEC SQL COMMIT WORK RELEASE; exit(0); }