We are porting Oracle Pro*C code to PostgreSQL ECPG. We had several Oracle stored procedures that were ported into PostgreSQL functions like:
db1.update_some_logic(double precision, double precision, text, text)
On the C file I've tried several things but nothing seems to work.
Option 1:
EXEC SQL SELECT db1.update_some_logic(10411, 920, 'TYT','N');
Error 1:
too few arguments on line 4379
Option 2:
EXEC SQL BEGIN DECLARE SECTION;
const char *stmt2 = "SELECT db1.update_some_logic(10411, 920, 'TYT','N');";
EXEC SQL END DECLARE SECTION;
EXEC SQL EXECUTE IMMEDIATE :stmt2;
Error 2:
too few arguments on line 4384
The function clearly has 4 arguments, not sure what I am missing here.
I can reproduce the issue with PostgreSQL 12.3.
I have found following workaround:
the function should not return void but a value for example an integer
the ECPG program must execute the function and get the returned value with SELECT ... INTO:
EXEC SQL SELECT update_some_logic(10411, 920, 'TYT','N') INTO :v_key;
You'll need work with share variables for those parameters.
EXEC SQL BEGIN DECLARE SECTION;
double parameter_1;
double parameter_2;
char parameter_3[100];
char parameter_4[100];
int result; //I guess your function return int??
EXEC SQL END DECLARE SECTION;
EXEC SQL SELECT update_some_logic(:parameter_1, :parameter_2, :parameter_3, :parameter_4) INTO :result;
if (sqlca.sqlcode!=0)
{
printf("Error: %ld\n", sqlca.sqlcode);
printf("Message:%s\n", sqlca.sqlerrm.sqlerrmc);
}
printf("It works %d\n", result);
I am trying to build a C program to generate partly random strings and insert a large number of them to db2 rows in a loop so I need it to insert from embedded SQL.... The program does everything except inserts.
I can make a select query, c compiler, db2 precompiler and binder all execute with no errors.
It does not even let me make an insert with hard coded variables let alone with host variables and the query is perfectly successful from terminal line
db2 "INSERT INTO
SYT006_COUNTRY(SL_ISO2,BZ_COUNTRY,KZ_RISK)
VALUES
('XY', 'wont work from embedded SQL', 'R')"
Host variables defined as char arrays with one more position for terminator. Platform is Ubuntu.
I tried a million things so here is the code.. EDIT everything obsolete cleared
#include <stdio.h>
#include <string.h>
#include <sqlca.h>
#include <stdlib.h>
#include <sqlenv.h>
#include <sqlcodes.h>
#include <sqlutil.h>
#include <time.h>
#include "utilemb.h"
#include "sqlaprep.h"
/* SQL includes */
EXEC SQL INCLUDE SQLCA;
EXEC SQL BEGIN DECLARE SECTION;
EXEC SQL DECLARE SYT006_COUNTRY TABLE
(
KEYFIELD INT,
SL_ISO2 CHAR(2) NOT NULL,
BZ_COUNTRY CHAR(30) NOT NULL,
KZ_RISK CHAR(1) NOT NULL
) ;
char hostVar[31];
EXEC SQL END DECLARE SECTION;
int main()
{
/* connect to the database REQUIRED */
printf("Connecting to database...\n ");
EXEC SQL CONNECT TO "sample";
if (SQLCODE <0)
{
printf("Connect Error. Code: %d\n", sqlca.sqlcode);
}
else
{
printf("Connected to database. Code: %d\n",sqlca.sqlcode );
}
/* works only from command line terminal*/
EXEC SQL
INSERT INTO
SYT006_COUNTRY(SL_ISO2,BZ_COUNTRY,KZ_RISK)
VALUES
('XY', 'please', 'R');
/* this one works perfectly normal*/
/*
EXEC SQL SELECT BZ_COUNTRY INTO :hostVar
FROM SYT006_COUNTRY WHERE KEYFIELD = 515;
printf("printing hostVar: %s\n", hostVar);
*/
printf("MAIN FINISH \n");
return (0);
}/*end main*/
the only out of order thing I get is when i start db2 from my shell script:
SQL5043N Support for one or more communications protocols specified in the DB2COMM environment variable failed to start successfully. However, core database manager functionality started successfully.
SQL1063N DB2START processing was successful.
Nothing better then a cup of coffee the next day.
Embedded-SQL program requires the line
EXEC SQL COMMIT;
I want to import to read and convert a database in .idb format . I was told that this file comes from a IBM tool used on indows 98. I do not know exactly from which tool.
I have the following files
a .idb file of 1Go size which was called directly by the application using this database with this call procedure
SERVER NAME=localhost:C:\BELAIR3\APPSER\APPSER.IDB
a .db file
a .lck file
a .mb file
a .px file
a .sql file with some procedures, here is the beginning:
CONNECT "localhost:C:\BELAIR3\appser\APPSER.IDB"
USER SYSDBA PASSWORD masterkey;
set term ^ ;
CREATE PROCEDURE CHERCHENOMDPP(
NUMERO INTEGER
)
RETURNS (
NOMDPP VARCHAR(50)
)
AS
DECLARE VARIABLE vnom VARCHAR(50);
DECLARE VARIABLE vprenom VARCHAR(50);
DECLARE VARIABLE vnombre INTEGER;
BEGIN
SELECT b_nom,b_prenom FROM DPPSER
WHERE b_numdpp = :numero
INTO :vnom, :vprenom;
if (vnom is null) then
nomdpp = "???";
else if (vprenom is null) then
nomdpp = vnom;
else
nomdpp = vnom||" "||vprenom;
SUSPEND;
END ^
I am using using postgresql server on my laptop, and trying to connect with the database through my C program, using epcg. I wrote the following commands to precompile, compile and run the my .pgc program.
PRE-COMPIILE - epcg sql.pgc
COMPILE - gcc -c sql.c -I/usr/include/postgresql
RUN - gcc -o sql sql.o -L/usr/lib -lecpg
My program is getting compiled and run successfully, i.e., it is showing no errors.
But, when I try to retrieve my database at the command line prompt, using command,
COMMAND - psql database
No updates are there in the tables,i.e., the commands which I have written in the program, are not getting updated in the database.
The following is the code in .pgc file :
#include<stdio.h>
int main()
{
EXEC SQL CONNECT TO database;
EXEC SQL create table player(player_id int,player_name varchar(255),team varchar(10));
EXEC SQL create table player1(player_id int,player_name varchar(255),team varchar(10));
EXEC SQL INSERT INTO player VALUES(1,'ram','a');
EXEC SQL COMMIT;
EXEC SQL DISCONNECT database;
return 0;
}
The following is the code in C, after precompilation :
/* Processed by ecpg (4.7.0) */
/* These include files are added by the preprocessor */
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
/* End of automatic include section */
#line 1 "sql.pgc"
#include<stdio.h>
int main()
{
{ ECPGconnect(__LINE__, 0, "vidisha#localhost:5432" , NULL, NULL , NULL, 0); }
#line 8 "sql.pgc"
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table player ( player_id int , player_name varchar ( 255 ) , team varchar ( 10 ) )", ECPGt_EOIT, ECPGt_EORT);}
#line 10 "sql.pgc"
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table player1 ( player_id int , player_name varchar ( 255 ) , team varchar ( 10 ) )", ECPGt_EOIT, ECPGt_EORT);}
#line 11 "sql.pgc"
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into player values ( 1 , 'ram' , 'a' )", ECPGt_EOIT, ECPGt_EORT);}
#line 12 "sql.pgc"
{ ECPGtrans(__LINE__, NULL, "commit");}
#line 14 "sql.pgc"
{ ECPGdisconnect(__LINE__, "vidisha");}
#line 16 "sql.pgc"
return 0;
}
Using the script you wrote I didn't get any changes in my test DB too.
So, using #vector advice, if I gave the username of my test DB, everything works:
Using this makefile:
default:
ecpg sql.pgc
gcc -c sql.c -I/usr/include/postgresql
gcc -o sql sql.o -L/usr/lib -lecpg
And, as an example, I'll use:
testdb as the database name
9876 as the port
testuser as the username
testpass as the password
with this sql.pgc:
#include <stdio.h>
int main() {
EXEC SQL CONNECT TO testdb#localhost:9876 USER 'testuser' IDENTIFIED BY 'testpass';
EXEC SQL create table player(player_id int,player_name varchar(255),team varchar(10));
EXEC SQL create table player1(player_id int,player_name varchar(255),team varchar(10));
EXEC SQL INSERT INTO player VALUES(1,'ram','a');
EXEC SQL COMMIT;
EXEC SQL DISCONNECT;
return 0;
}
everything worked well.
You also have to provide the password and the default port is 5432.
This code works fine for me.
testdb- databaseName, postgres - username, 123321 - password
_Also after once executing this program you have to remove both the create table lines else your code will not work (as the tables are alredy created). You will get no error as you have not done error handling. To learn about error handling and more click here. _
#include<stdio.h>
int main()
{
EXEC SQL CONNECT TO testdb#localhost:5432 USER 'postgres' USING '123321';
EXEC SQL create table player(player_id int,player_name varchar(255),team varchar(10));
EXEC SQL create table player1(player_id int,player_name varchar(255),team varchar(10));
EXEC SQL INSERT INTO player VALUES(1,'ram','a');
EXEC SQL COMMIT;
EXEC SQL DISCONNECT database;
return 0;
}
You have to set search path to your current database.
EXEC SQL SET SEARCH_PATH TO "your database";
Also, at the end, don't forget to commit.
EXEC SQL COMMIT;
I have the below code in my .pc file which is called by almost 10 processes but for one process its not working as showing "Core dump/Segmentation Fault" .
This is the code which is called by every process as the first step to connect to Database.
Please suggest where is the problem in the below code -:
void DatabaseLogon (void)
{
EXEC SQL BEGIN DECLARE SECTION;
char *pchORALOG="";
EXEC SQL END DECLARE SECTION;
EXEC SQL WHENEVER SQLERROR DO SQLError();
/* Save text of current SQL statement in the ORACA if an error occurs. */
oraca.orastxtf = ORASTFERR;
/* try to get the oracle login user/pass at the process level */
if ( (pchORALOG=getenv("oralog")) == NULL )
{
printf("Error:Cannot Logon to database!\n");
SQLError();
}
else
{
EXEC SQL CONNECT :pchORALOG;
}
} /* End Of DatabaseLogon */
Thanks
Arpita
My ProC connect statement looks like this:
EXEC SQL CONNECT :user IDENTIFIED BY :passwd USING :database;
you are trying to modify a read only location here.
pchORALOG=getenv("oralog")
below statement is the problem:
char *pchORALOG="";
change it to:
char *pchORALOG;