📜 ⬆️ ⬇️

Data acquisition from Oracle DBMS using C / C ++ under SuSE Linux

Hi habrachelovek!

Faced the other day with the need to get data from Oracle DBMS using C / C ++ under SuSE Linux

Prior to this, I have repeatedly performed similar things from other languages ​​(Perl, PHP, sh), but never from C.
Of all the available tools that allow you to connect to Oracle, I stopped at the method of precompiling proc, the binary of which is included in the basic configuration of the Oracle client.

')
The system on which the riot of colors will occur:
vmotp # cat / etc / SuSE-release
SUSE Linux Enterprise Server 11 (i586)
VERSION = 11
PATCHLEVEL = 0
+
oracle.client VER: 10.2.0.1.0


The database itself is on a different machine. Server version: 9.0.2 (I know that it is old, but it serves exclusively as a historical replica and a simple base for temporary tables)

First, let's make an example of the similarity “Hello World”, which will display the result of the query
SELECT 12345,'TEST STRING' FROM dual;

Make a file test_ora.pc with the following content

#include <ctype.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>

#define ORA_PARAM " ORACLE_LOGIN/ORACLE_PASS@ORACLE_SID " /* */

EXEC SQL BEGIN DECLARE SECTION; /* END DECLARE SECTION , Oracle*/
static int ora_i; /* INT */
static VARCHAR ora_str[20]; /* CHAR */
static VARCHAR connect_str[100]; /* */
EXEC SQL END DECLARE SECTION;

EXEC SQL INCLUDE SQLCA; /* SQLCA ( ) */

int main( int argc, char* argv[] )
{
char str[21]; /* CHAR */
memset(str,0,sizeof(str)); /* */
connect_str.len = sprintf(connect_str.arr,ORA_PARAM); /* */
EXEC SQL CONNECT :connect_str; /**/
EXEC SQL WHENEVER SQLERROR CONTINUE;
EXEC SQL SELECT 12345,'TEST STRING' INTO :ora_i,:ora_str FROM dual; /* , */
sprintf( str, "%*.*s", ora_str.len, ora_str.len, ora_str.arr ); /* CHAR C */
printf("\ni=[%d], str=[%s]\n", ora_i, str ); /* */
return 0;
} /* end of main() */


Now you need to “feed” our proc utility file, by default it is located along the path $ ORACLE_HOME / bin /
/opt/oracle/product/11gR1/db/bin/proc CODE=ANSI_C include=/opt/oracle/product/11gR1/db/lib include=/usr/lib/gcc/i586-suse-linux/4.3/include/ ireclen=4800 oreclen=4800 select_error=no release_cursor=no hold_cursor=yes ltype=none cpp_suffix=c USERID= ORACLE_LOGIN/ORACLE_PASS@ORACLE_SID SQLCHECK=SEMANTICS iname=test_ora.pc;


cc test_ora.c -L/opt/oracle/product/11gR1/db/lib /opt/oracle/product/11gR1/db/lib/libclntsh.so /opt/oracle/product/11gR1/db/lib/libsqlplus.a -o test_ora;

Run the resulting file and see that the focus was a success:
vmotp$ ./test_ora

i=[12345], str=[TEST STRING]
vmotp$


Ps. This article is published at the request of my friend. Who does not have a login for Habr, if someone has invites left, please share (lanarion (at) gmail.com).

Source: https://habr.com/ru/post/90556/


All Articles