Home » Developer & Programmer » Precompilers, OCI & OCCI » How can I compile
How can I compile [message #483219] Fri, 19 November 2010 04:28 Go to next message
Rafeek
Messages: 159
Registered: April 2007
Location: egypt
Senior Member
I wrote the below C code and I don't How can compile to so file

/* Include standard IO. */
#include <stdio.h>
#include <string.h>
#include <sqlca.h>
#include <stdlib.h>
/* Declare a writestr function. */
void writestr(char *path, char *message)
{
/* Declare a FILE variable. */
FILE *file_name;
/* Open the file in write-only mode. */
file_name = fopen(path,"w");
/* Write to file the message received. */
fprintf(file_name,"%s\n",message);
/* Close the file. */
fclose(file_name);
EXEC SQL INSERT INTO XXC(COL1) VALUES(123);
        EXEC SQL COMMIT;
}

[Updated on: Fri, 19 November 2010 04:29]

Report message to a moderator

Re: How can I compile [message #483246 is a reply to message #483219] Fri, 19 November 2010 08:04 Go to previous messageGo to next message
Michel Cadot
Messages: 68624
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
What is your platform?
Maybe you should read the Pro*C/C++ Programmer's Guide, at least the "Frequently Asked Questions" section.

Regards
Michel

Re: How can I compile [message #483349 is a reply to message #483219] Sun, 21 November 2010 00:09 Go to previous messageGo to next message
Rafeek
Messages: 159
Registered: April 2007
Location: egypt
Senior Member
I use Oracle 11g under Linux.

first I wrote a small code to insert value in XXC table

#include <stdio.h>
#include <string.h>
#include <sqlda.h>
#include <sqlcpr.h>

EXEC SQL BEGIN DECLARE SECTION;
VARCHAR uid[30];
VARCHAR pwd[30];
EXEC SQL END DECLARE SECTION;
EXEC SQL INCLUDE SQLCA.H;

int main()
{
strcpy(uid.arr,"apps");
uid.len =strlen(uid.arr);
strcpy(pwd.arr,"apps");
pwd.len = strlen(pwd.arr);
EXEC SQL WHENEVER sqlerror goto errexit;
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
printf("\nConnected to Oracle8i using APPS/APPS\n");

EXEC SQL INSERT INTO XXC(COL1) VALUES(123);
printf("\nInsert into XXC \n");

EXEC SQL COMMIT WORK RELEASE;
printf("\n  COMMIT WORK RELEASE  \n");
return 1;
errexit:
printf("\nConnection failed\n");
return 1;
}


I use the flowing commands to compile.

$ proc iname=t.pc 
$ gcc -c t.c -I /oracle/TEST/db/db/tech_st/11.1.0/precomp/public
$ gcc -o t.so t.o -L /oracle/TEST/db/db/tech_st/11.1.0/lib -lclntsh


I run the code by the flowing command (it is work and insert value in XXC Table)
$ ./t.so



I wrote anther code by the same way to call from PL-SQL
code below

/* Include standard IO. */
#include <stdio.h>
#include <string.h>
#include <sqlca.h>
#include <stdlib.h>
#include <sqlda.h>
#include <sqlcpr.h>
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR uid[30];
VARCHAR pwd[30];
EXEC SQL END DECLARE SECTION;

void writestr(char *path, char *message)
{
strcpy(uid.arr,"apps");
uid.len =strlen(uid.arr);
strcpy(pwd.arr,"apps");
pwd.len = strlen(pwd.arr);

EXEC SQL WHENEVER sqlerror goto errexit;
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
EXEC SQL INSERT INTO XXC(COL1) VALUES(123);
EXEC SQL COMMIT WORK RELEASE;

FILE *file_name;
file_name = fopen(path,"w");
fprintf(file_name,"%s\n",message);
fclose(file_name);
errexit:
printf("\nConnection failed\n");
}


I use the flowing commands to compile.
$ proc iname=wr.pc
$ gcc -c wr.c -I /oracle/TEST/db/db/tech_st/11.1.0/precomp/public
$ gcc -shared -o wr.so wr.o -L /oracle/TEST/db/db/tech_st/11.1.0/lib -lclntsh


PL SQL Code
1- Create library
CREATE OR REPLACE LIBRARY library_write_string AS
'/oracle/TEST/db/db/tech_st/11.1.0/bin/wr.so';


2- Create procedure
CREATE OR REPLACE PROCEDURE write_string (PATH VARCHAR2, MESSAGE VARCHAR2)
AS
   EXTERNAL
   LIBRARY library_write_string
   NAME "writestr"
   PARAMETERS (PATH STRING, MESSAGE STRING);


3- Calling
DECLARE
   l   NUMBER;
BEGIN
   write_string ('/tmp/r.txt', 'rafeek say Hiiiiiii');
   COMMIT;

   SELECT COUNT ('X')
     INTO l
     FROM xxc;

   DBMS_OUTPUT.put_line ('XX==>' || l);
END;


At the end I can not Insert value in XXC when I tried to call wr.so from PL/SQL code.


Thanks for Help

Rafeek

[Updated on: Sun, 21 November 2010 00:10]

Report message to a moderator

Re: How can I compile [message #486721 is a reply to message #483219] Sun, 19 December 2010 09:16 Go to previous message
Rafeek
Messages: 159
Registered: April 2007
Location: egypt
Senior Member
I found the issue to work with database should write database username,password,port and SID

#include <stdio.h>
#include <string.h>
#include <sqlca.h>
#include <stdlib.h>
#include <sqlda.h>
#include <sqlcpr.h>

EXEC SQL BEGIN DECLARE SECTION;
VARCHAR uid[30];
VARCHAR pwd[30];
EXEC SQL END DECLARE SECTION;

#define NAME_LEN 30 
#define ERR_LEN 512 

VARCHAR username[NAME_LEN]; 
VARCHAR password[NAME_LEN]; 
VARCHAR database[NAME_LEN]; 



/* Declare a writestr function. */
void writestr(char *path, char *message)
{

strncpy((char *) username.arr, "apps", NAME_LEN); 
  username.len = (unsigned short) strlen((char *) username.arr); 

  strncpy((char *) password.arr, "apps", NAME_LEN); 
  password.len = (unsigned short) strlen((char *) password.arr); 

  strncpy((char *) database.arr, "reeora.raya.com:1522/TEST", NAME_LEN); 
  database.len = (unsigned short) strlen((char *) database.arr);

EXEC SQL WHENEVER sqlerror goto errexit;

EXEC SQL CONNECT :username IDENTIFIED BY :password USING :database;
EXEC SQL INSERT INTO XXC(COL1) VALUES('SYSDATE==>'||TO_CHAR(sysdate,'DD-MON-RRRR HH24:MI:SS'));

EXEC SQL COMMIT WORK RELEASE;



/* Declare a FILE variable. */
FILE *file_name;
/* Open the file in write-only mode. */
file_name = fopen(path,"w");
/* Write to file the message received. */
fprintf(file_name,"%s\n",message);
/* Close the file. */
fclose(file_name);

errexit:
printf("\nConnection failed\n");


}


compile Commands :

1-create c file from PC 
	 proc iname=wr.pc

2-compile C to o file
	gcc -c wr.c -I /oracle/TEST/db/db/tech_st/11.1.0/precomp/public

3-link the result object modules with modules in SQLLIB 
	 gcc -shared -o wr.so wr.o -L /oracle/TEST/db/db/tech_st/11.1.0/lib -lclntsh
	

[Updated on: Sun, 19 December 2010 09:18]

Report message to a moderator

Previous Topic: Data lost after adding Null terminator for VARCHAR data recieved from database
Next Topic: Pro*Cobol get ORA-01458: invalid length inside variable character string
Goto Forum:
  


Current Time: Thu Mar 28 06:14:27 CDT 2024