/* * sample3.pc * Host Arrays * * This program connects to ORACLE, declares and opens a cursor, * fetches in batches using arrays, and prints the results using * the function print_rows(). */ #include #include EXEC SQL INCLUDE sqlca; EXEC SQL BEGIN DECLARE SECTION; #define NAME_LENGTH 20 #define ARRAY_LENGTH 5 /* Another way to connect. */ char *username = "ユーザーネーム"; char *password = "パスワード"; /* Declare a host structure tag. */ struct { int empid[ARRAY_LENGTH]; char empname[ARRAY_LENGTH][NAME_LENGTH]; char empdesgn[ARRAY_LENGTH][NAME_LENGTH]; float empsalary[ARRAY_LENGTH]; } emp_rec; /* Declare this program's functions. */ void print_rows(); /* produces program output */ void sql_error(); /* handles unrecoverable errors */ EXEC SQL END DECLARE SECTION; int main() { int num_ret; /* number of rows returned */ /* Connect to ORACLE. */ EXEC SQL WHENEVER SQLERROR DO sql_error("Connect error:"); EXEC SQL CONNECT :username IDENTIFIED BY :password; printf("\nConnected to ORACLE as user: %s\n", username); EXEC SQL WHENEVER SQLERROR DO sql_error("Oracle error:"); /* Declare a cursor for the FETCH. */ EXEC SQL DECLARE c1 CURSOR FOR SELECT * FROM EmployeeDetails; EXEC SQL OPEN c1; /* Initialize the number of rows. */ num_ret = 0; /* Array fetch loop - ends when NOT FOUND becomes true. */ EXEC SQL WHENEVER NOT FOUND DO break; for (;;) { EXEC SQL FETCH c1 INTO :emp_rec; /* Print however many rows were returned. */ print_rows(sqlca.sqlerrd[2] - num_ret); num_ret = sqlca.sqlerrd[2]; /* Reset the number. */ } /* Print remaining rows from last fetch, if any. */ if ((sqlca.sqlerrd[2] - num_ret) > 0) print_rows(sqlca.sqlerrd[2] - num_ret); EXEC SQL CLOSE c1; printf("\nAu revoir.\n\n\n"); /* Disconnect from the database. */ EXEC SQL COMMIT WORK RELEASE; return 0; } void print_rows(n) int n; { int i; printf("\nNumber Employee Salary"); printf("\n------ -------- ------\n"); for (i = 0; i < n; i++) printf("%-9d%-15.15s%-15.15s%9.2f\n", emp_rec.empid[i], emp_rec.empname[i], emp_rec.empdesgn[i], emp_rec.empsalary[i]); } void sql_error(msg) char *msg; { EXEC SQL WHENEVER SQLERROR CONTINUE; printf("\n%s", msg); printf("%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc); EXEC SQL ROLLBACK WORK RELEASE; }