1 Reply Latest reply on Aug 31, 2007 9:52 AM by Sudheendra-Oracle

    pro*c  :Segmentation fault

    537155
      hi everyone ,i got Segmentation fault errors ,when run the sample2.pc
      os:fedora6
      database:oracle10g

      program is follow

      /*
      * sample2.pc
      *
      * This program connects to ORACLE, declares and opens a cursor,
      * fetches the names, salaries, and commissions of all
      * salespeople, displays the results, then closes the cursor.
      */

      #include <stdio.h>
      #include <sqlca.h>

      #define UNAME_LEN 20
      #define PWD_LEN 40

      /*
      * Use the precompiler typedef'ing capability to create
      * null-terminated strings for the authentication host
      * variables. (This isn't really necessary--plain char *'s
      * does work as well. This is just for illustration.)
      */
      typedef char asciiz[PWD_LEN];

      EXEC SQL TYPE asciiz IS STRING(PWD_LEN) REFERENCE;
      asciiz username;
      asciiz password;


      /* Declare function to handle unrecoverable errors. */
      void sql_error();


      main()
      {

      EXEC SQL BEGIN DECLARE SECTION;

      VARCHAR username[10];

      VARCHAR password[10];

      VARCHAR server[30];

      struct emp_info
      {
      char emp_name[40];
      float salary;
      float commission;
      };

      struct emp_info *emp_rec_ptr;

      EXEC SQL END DECLARE SECTION;

      /* Allocate memory for emp_info struct. */
      if ((emp_rec_ptr =
      (struct emp_info *) malloc(sizeof(struct emp_info))) == 0)
      {
      fprintf(stderr, "Memory allocation error.\n");
      exit(1);
      }

      /* Connect to ORACLE. */
      /*
      strcpy(username, "SCOTT");
      strcpy(password, "TIGER");
      */


      strcpy(username.arr,"scott");

      username.len=strlen(username.arr);

      strcpy(password.arr,"tiger");

      password.len=strlen(password.arr);

      strcpy(server.arr,"ora9");

      server.len=strlen(server.arr);


      EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--");

      EXEC SQL DECLARE db_name DATABASE;
      EXEC SQL CONNECT :username IDENTIFIED BY :password AT db_name USING :server;
      //EXEC SQL CONNECT :username IDENTIFIED BY :password;

      printf("\nConnected to ORACLE as user: %s\n", username);

      /* Declare the cursor. All static SQL explicit cursors
      * contain SELECT commands. 'salespeople' is a SQL identifier,
      * not a (C) host variable.
      */
      EXEC SQL DECLARE salespeople CURSOR FOR
      SELECT ENAME, SAL, COMM
      FROM EMP
      WHERE JOB LIKE 'SALES%';

      /* Open the cursor. */
      EXEC SQL OPEN salespeople;

      /* Get ready to print results. */
      printf("\n\nThe company's salespeople are--\n\n");
      printf("Salesperson Salary Commission\n");
      printf("----------- ------ ----------\n");

      /* Loop, fetching all salesperson's statistics.
      * Cause the program to break the loop when no more
      * data can be retrieved on the cursor.
      */
      EXEC SQL WHENEVER NOT FOUND DO break;

      for (;;)
      {
      EXEC SQL FETCH salespeople INTO :emp_rec_ptr;
      printf("%-11s%9.2f%13.2f\n", emp_rec_ptr->emp_name,
      emp_rec_ptr->salary, emp_rec_ptr->commission);
      }

      /* Close the cursor. */
      EXEC SQL CLOSE salespeople;

      printf("\nArrivederci.\n\n");

      EXEC SQL COMMIT WORK RELEASE;
      exit(0);
      }



      void
      sql_error(msg)
      char *msg;
      {
      char err_msg[512];
      int buf_len, msg_len;

      EXEC SQL WHENEVER SQLERROR CONTINUE;

      printf("\n%s\n", msg);

      /* Call sqlglm() to get the complete text of the
      * error message.
      */
      buf_len = sizeof (err_msg);
      sqlglm(err_msg, &buf_len, &msg_len);
      printf("%.*s\n", msg_len, err_msg);

      EXEC SQL ROLLBACK RELEASE;
      exit(1);
      }