/* Copyright © 1995, 1997, Oracle Corporation. All Rights Reserved. */ /* ABSTRACT: * * This program demonstrates the use of the SQL precompiler for the C * language to load an Oracle Rdb database from a stream (flat) file. * * This program attaches to an existing Oracle Rdb database, opens a data * file containing college records, and reads the records, formatting and * inserting them into the database until the end of the date file * is reached. Then the program commits the transaction. */ #include #include #include #include #ifdef VMS #include "sql$sample:sql_load_rtl.sc" #endif #if defined(__osf__) || defined (_WIN32) #include #endif main( ) { /* Fields to receive strings read from the sql_colleges.dat file record */ char col_code[5]; char col_name[26]; char city[21]; char state[3]; char zip_code[6]; /* File definitions for reading the sql_colleges.dat file */ FILE *colleges_file; /* Declarations for error handling */ int return_status; /* Define the SQLCA. */ EXEC SQL INCLUDE SQLCA; /* Declare the database. */ EXEC SQL DECLARE ALIAS FILENAME personnel; /* Set up error handling for failures on execution of SQL statements. */ EXEC SQL WHENEVER SQLERROR GOTO HANDLE_ERROR; /* Operator message to the terminal */ printf("\nProgram: Loading COLLEGES"); /* Open the sequential file containing the colleges data records. */ #ifdef VMS colleges_file = fopen("sql$sample:sql_colleges.dat","r"); #endif #if defined(__osf__) || defined (_WIN32) colleges_file = fopen("sql_colleges.dat","r"); #endif /* This procedure uses the executable form for starting a transaction. */ EXEC SQL SET TRANSACTION READ WRITE RESERVING COLLEGES FOR EXCLUSIVE WRITE; /* Main loop until data file is empty */ while (get_line(colleges_file) != NULL) { get_field(colleges_file,col_code,4); get_field(colleges_file,NULL ,3); get_field(colleges_file,col_name,25); get_field(colleges_file,NULL ,3); get_field(colleges_file,city ,20); get_field(colleges_file,NULL ,3); get_field(colleges_file,state ,2); get_field(colleges_file,NULL ,3); get_field(colleges_file,zip_code,5); /* INSERT a row in the COLLEGES table. * The list of names in the VALUES clause corresponds to the * host variables containing the values. The list of names that * follows the INSERT clause names the columns in the table * that are to be inserted. */ EXEC SQL INSERT INTO COLLEGES (COLLEGE_CODE, COLLEGE_NAME, CITY, STATE, POSTAL_CODE) VALUES (:col_code, :col_name, :city, :state, :zip_code); } /* Commit the transaction. */ EXEC SQL COMMIT; /* Close the sql_colleges.dat data file. */ fclose(colleges_file); /* Operator prompt message */ printf("\nProgram: COLLEGES Loaded. Normal End-of-Job"); exit(1); /* Error handler for SQL errors */ HANDLE_ERROR: sql_signal(); exit(0); }