Home » Developer & Programmer » Precompilers, OCI & OCCI » pro*c :Segmentation fault
pro*c :Segmentation fault [message #263793] Fri, 31 August 2007 01:26 Go to next message
xtuyaowu
Messages: 1
Registered: August 2007
Junior Member
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);
}
Re: pro*c :Segmentation fault [message #263880 is a reply to message #263793] Fri, 31 August 2007 03:59 Go to previous message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
First,
Please read and follow OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format. Use the "Preview Message" button.
Please always post your Oracle version (4 decimals).

Then, you don't need to post a standard script/program from Oracle, its path in the Oracle installation directory.

Finally, you should better post the command you executed.

Regards
Michel
Previous Topic: Proc on Windows 2000
Next Topic: PCC-S-02201 occured while compiling proc file
Goto Forum:
  


Current Time: Thu Mar 28 16:11:58 CDT 2024