The following example demonestrate how to Query a table using Oracle Pro*c Pre-compiler. In the example I have used the table employees. Following are the Steps.
Create a file CursorExample.pc and past the following contents.
Compile the program.
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sqlca.h>
- #include <sqlda.h>
- #include <sqlcpr.h>
- #define NAME_LEN 30
- #define ERR_LEN 512
- VARCHAR username[NAME_LEN];
- VARCHAR password[NAME_LEN];
- VARCHAR database[NAME_LEN];
- /*
- ** host variables to hold results of query
- */
- EXEC SQL BEGIN DECLARE SECTION;
- struct {
- int empId;
- VARCHAR firstName[NAME_LEN];
- VARCHAR lastName[NAME_LEN];
- int salary;
- int deptNo;
- } empRec;
- struct
- {
- short emp_name_ind;
- short sal_ind;
- short comm_ind;
- } empRecInd;
- int empId;
- VARCHAR firstName[NAME_LEN];
- VARCHAR lastName[NAME_LEN];
- int salary;
- int deptNo;
- EXEC SQL END DECLARE SECTION;
- /*
- ** standard Pro*C error handler function
- */
- void sql_error(char *msg)
- {
- char err_msg[ERR_LEN];
- size_t buf_len, msg_len;
- EXEC SQL WHENEVER SQLERROR CONTINUE;
- printf("\n");
- if (msg)
- {
- printf("%s\n", msg);
- }
- buf_len = sizeof (err_msg);
- sqlglm(err_msg, &buf_len, &msg_len);
- printf("%.*s", msg_len, err_msg);
- EXEC SQL ROLLBACK RELEASE;
- exit(EXIT_FAILURE);
- }
- int main(int argc, char *argv[])
- {
- strncpy((char *) username.arr, "username", NAME_LEN);
- username.len = (unsigned short) strlen((char *) username.arr);
- strncpy((char *) password.arr, "password", NAME_LEN);
- password.len = (unsigned short) strlen((char *) password.arr);
- strncpy((char *) database.arr, "ip address:port_no/database_name", NAME_LEN);
- database.len = (unsigned short) strlen((char *) database.arr);
- /*
- ** register the error handler function
- */
- EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error: \n");
- /*
- * attempt to connect to the database
- */
- EXEC SQL CONNECT :username IDENTIFIED BY :password USING :database;
- /*
- ** indicate we want to break out of the loop when we get a no data
- ** found message (i.e. at end of fetch)
- */
- EXEC SQL WHENEVER NOT FOUND DO break;
- /*
- ** declare and open cursor to fetch the employee details
- */
- EXEC SQL DECLARE emp_cursor CURSOR FOR
- SELECT employee_id,first_name,last_name,salary,department_id
- FROM employees WHERE ROWNUM<=10;
- EXEC SQL OPEN emp_cursor;
- /*
- ** loop through cursor fetching results
- ** and printing as we go
- */
- /*
- ** indicate we want to break out of the loop when we get a no data
- ** found message (i.e. at end of fetch)
- */
- EXEC SQL WHENEVER NOT FOUND DO break;
- printf("%-15s%-30s%-30s%-15s%-15s\n", "EmployeeId", "FirstName","LastName", "Salary","DepartmentNo");
- printf("------------------------------------------------------------------------------------------------------\n");
- for (;;)
- {
- EXEC SQL FETCH emp_cursor
- INTO :empRec;
- empRec.firstName.arr[empRec.firstName.len] = '\0';
- empRec.lastName.arr[empRec.lastName.len]= '\0';
- printf("%-15d%-30s%-30s%-15d%-15d\n", empRec.empId, empRec.firstName.arr,empRec.lastName.arr, empRec.salary, empRec.deptNo);
- }
- /*
- ** clean-up and exit
- */
- EXEC SQL CLOSE emp_cursor;
- EXEC SQL COMMIT RELEASE;
- return EXIT_SUCCESS;
- }
- $ proc CursorExample.pc
- This will create CursorExample.c file
- Compile the CursorExample.c
- $ gcc -c CursorExample.c -I/$ORACLE_HOME/precomp/public/
- $ gcc CursorExample.o -o CursorExample -I$ORACLE_HOME/precomp/public/ -I$ORACLE_HOME/rdbms/public/ -L$ORACLE_HOME/lib/ -L$ORACLE_HOME/precomp/lib/ -lclntsh
- This will generate an Executable file CursorExample.
- Execute the process by running the following command.
- $ ./CursorExample
- Output
- --------
- EmployeeId FirstName LastName Salary DepartmentNo
- ------------------------------------------------------------------------------------------------------
- 100 Steven King 240001 90
- 101 Neena Kochhar 17000 90
- 102 Lex De Haan 17000 90
- 103 Alexander Hunold 9000 60
- 104 Bruce Ernst 6000 60
- 105 David Austin 4800 60
- 106 Valli Pataballa 4800 60
- 107 Diana Lorentz 4200 60
- 108 Nancy Greenberg 12008 100
- 109 Daniel Faviet 9000 100
No comments:
Post a Comment