Oracle's C API

zhaozj2021-02-16  132

Oracle Application Linux Development C

Chen Yue

With the continuous improvement and development of the Linux operating system, there have been a large number of LINUX platform applications, and the original Unix platform-based commercial software is constantly being ported to Linux. Topic, Oracle announced that all of his existing and future database products and business applications will support Linux platforms. The C language library of the OCI for Linux described herein is the C language interface of Oracle on the Linux platform.

We know that in a complex Oracle database application, the C program code is often added to the core layer module of its business logic due to its flexibility, efficiency of its language itself. Oracle Database The interface of the C language is OCI (Oracle CommON Interface) C-Library, which is a very powerful database operation module. It supports transaction processing, multi-connections in single transactions, support data access, and a series of advanced applications such as object access, storage procedures, and the various additional products under Oracle provide interfaces. But we found that in order to keep the OCI library maintain a unified style and consider down compatibility, Oracle has a large number of C language types and code, which makes the OCI library seems to be complicated. The user does not know where to start. The Libsqlora8 library developed by Kai Poitschke has initially solved this problem, which makes it easier to use in non-high-end C language under Linux.

Libsqlora8 for * NIX is an easy-to-use C language package developed by GNU / Linux organizations for Oracle8 OCI library. It performs a large number of OCI data types as a universal C language data type, re-categorize the OCI function by type, greatly reduces the call steps and program code amount of the function. Libsqlora8 has many leading features:

Easy to use dynamic SQL characteristics;

Repeat of the cursor with different variables in the same connection;

Multi-database connections in the same transaction;

BUILD-IN TRACE function in Oracle Database Application;

Correctly handle array variables in data insertion operation;

Multi-platform support Oracle 8.0.4 (HP-UX 9), Oracle 8.05 (GNU / Linux), Oracle 8.1.6 (GNU / Linux), etc .;

Can be linked into the application as a static or dynamic form.

Below we are subcompered in detail how to develop Oracle database applications on the Linux platform.

1. Install the Linux operating system and appropriate system configuration for the new system. In this example we selected the Redhat Linux 6.2 operating system. When partitioning is partitioning, we specifically divide two partitions: / u01, / u02, as an Oracle database installation point for the system software and database files. After installing the system, we add two new groups to the system: OinsTALL and DBA, and create a new user Oracle, he has the entire database system software. It will not be described in detail here.

2. Here we should install the Oracle database, this time we use Oracle 8.1.6 version, which has a good support for internationalization. Before installing the database, we must first make some settings for Oracle users. Mainly in the startup script of the user, add some necessary environment variables, in this case, you can set it as follows:

Oracle_base = / u01 / app / oracle

Oracle_Home = $ Oracle_Base / Product / 8.1.6

Oracle_sid = ORATEST

PATH = $ oracle_home / bin: / usr / bin: / etc: / bin: / usr / x11r6 / bin

LD_LIBRARY_PATH = $ ORACLE_HOME / LIBEXPORT ORACLE_BASE ORACLE_HOME ORACLE_SID PATH

3. Database installation of Oracle 8.1.6 is relatively simple, we choose the default installation, and complete the installation process in the system prompt. Note that Oracle8i's requirements are relatively high, especially memory, in some special applications, to modify the default settings of the system to improve database performance. The discussion on database tuning is not related to this article, there is no longer introduced in detail here. Start the database, ok, now we can log in to the database with SQLPlus, you can see that the default installation Oracle database has a class of OciteSt database tables, using these tables as the default table in our example.

4. Install the libsqlora8 library function. The library function The current version is libsqlora8-2.1.5, which can be obtained from many Linux websites or from

Http://www.china-linux.org Upload libsqlora8-2.1.5.tar.gz source package. Installation follows:

$> tar -xzvf libsqlora8-2.1.5.tar.gz

$> cd libsqlora8-2.1.5

$> Ld_library_path = $ oracle_home / lib

$> EXPORT LD_LIBRARY_PATH

$> ./ Configure

$> make

$> make INSTALL

For developers who want to use Oracle Build-in Trace, set the following environment variables, SQLORA_TRACE_LEVEL, SOLORA_TRACE_FILE, SQLORA_ARRAYSIZE, of course, oracle_sid is a must set.

5. Here we introduce the main functions of libsqlora8.

1) int SQLO_INIT (INT THREADED_MODE) Initialize the library interface, read the environment variable, set the corresponding global variable. Currently, Threaded_Mode is set to 0.

2) INT SQLO_CONNECT (INT * DBH, CHAR * Connect_STR) Connect the database, DBH is a database connection descriptor, and connect_str is a username / password string.

3) int SQLO_FINISH (INT DBH) Disconnects the database connection.

4) INT SQLO_OPEN (int DBH, CHAR * STMT, INT ARGC, CHAR * ARGV [] opens the cursor returned by the query statement determined by the STMT. Argc, Argv is the parameters of the query, and we will pass parameters with a clearer way.

5) Int SQLO_CLOSE (INT STH) Close the cursor opened by the previous function.

6) INT SQLO_FETCH (INT STH) Gets a record from the open cursor and stores it into a distributed memory space.

7) const char ** SQLO_VALUES (INT STH, INT * NUMBALUES, INT DOSTRIP) Returns the value obtained from the last SQLO_FETCH from the memory, which is returned in the form of a string.

8) The following introduces another search mode, int SQLO_PREPARE (INT DBH, CHAR Const * STMT), returns an open cursor STH.

9) int sqlo_bind_by_name (int sth, const char * param_name, int param_type, const void * param_addr, unsigned int param_size, short * ind_arr, int is_array) of the incoming query parameters, tied in the form of a function name of the variable set. If you use an array, the parameter param_addr and Ind_arr must point to the array. INT SQLO_BIND_BY_POS (int Star, int param_pos, int param_type, const line_si_addr, unsigned int param_size, short * inde_arr, int is_ARRAY) The outgoing value of the query statement is bound to the variables in the function in the function.

10) Int SQLO_EXECUTE (INT STH, IT IT IT IT IT IT ITs) Execute Query statements. "Iterations" can be set to "1".

11) After performing the database operation, we can use int SQLO_COMMIT (INT DBH) to run with int SQLO_ROLLBACK (INT DBH).

12) Libsqlora8 has some other operations, which is not listed here.

Here are several examples to illustrate how these functions are used.

CSTR = "OCITEST / OCITEST"; // User Name / Password

Status = SQLO_INIT (0);

IF (SQLO_SUCCESS! = STATUS)

{Printf ("SQL_INIT FAILED. EXITING / N");

Exit (1);

}

Status = SQLO_CONNECT (& DBH, CSTR); // Int DBH

The above source code shows how to connect the database.

/ * SELECT All and Display * /

Char * select_stmt = "SELECT CNAME, CLENGTH, Colid from OCICOLU";

IF (0> (SD = SQLO_Open (DBH, SELECT_STMT, 0, NULL)))

{Printf ("SQLO_OPEN FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

While (0 == SQLO_FETCH (SD, 1))

{v = SQLO_VALUES (SD, NULL, 1);

Printf ("RESULT:% S / N", V);

}

IF (0> SQLO_CLOSE (SD))

{Printf ("SQLO_OPEN FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

The above example shows the first inquiry method, obviously, this method is simpler, but not flexible enough.

Char * Update_Stmt =

"Update OciteSt.upload_log set upload_fresh = where log_name =: 1";

IF (0 <= (sth = SQLO_PREPARE (DBH, UPDATE_STMT)))))

{IF (SQLO_SUCCESS! =

(SQLO_BIND_BY_NAME (Sth, ": 1", SQLOT_STR, PACKET_NAME, 64, NULL, 0)

))

{Printf ("SQLO_BIND_PARAM FAILED FAILED:% S / N", SQLO_GETERROR (DBH)); RETURN 0;

}

}

IF (SQLO_SUCCESS! = SQLO_EXECUTE (Sth, 1))

{Printf ("SQLO_EXECUTE FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

The above code shows how to bind variables through the name, ": 1" is represented as a variable (name casual) in the Oracle SQL statement, binding to the packet_name variable in the SQLO_BIND_BY_NAME function. After the variable binding is completed, you can call the SQLO_EXECUTE function to perform this SQL statement.

Ok, we have introduced you to the basic method of use of libsqlora8. If you want to learn more, there is a detailed instructions and examples in libsqlora8 packages. You may wish to study it. What is your experience, welcome to contact me. E-mail:

Nick_chen@yeah.net

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------

* Testlora.c

* Test Programm for Libsqlora8 (Kai Poitschke)

* Assuming You Installed The Library with prefix = / usr / local, The Command

* To Compile this is:

* GCC -O Sample Sample.c -lsqlora8 -l $ oracle_home / lib -lclntsh

* ------------------------------------------------- ---------------------- * /

#include

#include

#include

#include "sqlora.h"

#define max_Iiters 10

#define max_loops 1 / * How Many Time We Run Tests * /

#define close_cursor 1

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------

* CREATE OUR TEST TABLE

* ------------------------------------------------- ---------------------- * /

INT CREATE_TABLE (INT DBH)

{

Int nkey;

Char ckey [6];

Double nval;

CHAR CVAL [21];

Char DVAL [11];

Int sth;

Char * create_table =

"CREATE TABLE T_SQLORA_TEST (/ N"

"Nkey Number (8) Not null, / n"

"CKEY VARCHAR2 (5) Not null, / n"

"NVAL Number (16, 4) NULL, / N"

"CVAL VARCHAR2 (20) NULL, / N"

"DVAL DATE";

/ * Check if the table already exists * /

IF (SQLO_NO_DATA == SQLO_EXISTS (DBH, "User_Tables", "Table_Name", "T_Sqlora_test", NULL)

{

/ * NO, CREATE IT * /

IF (SQLO_SUCCESS! = SQLO_EXEC (DBH, CREATE_TABLE))

{

Printf ("CREATE_TABLE FAILED:% S / N% S / N", SQLO_GETERROR (DBH),

CREATE_TABLE);

Return 0;

}

Printf ("Table T_SQLORA_TEST CREATED / N");

}

Return 1;

}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------

* Query The Test Table

* ------------------------------------------------- ---------------------- * /

INT Do_SELECT (INT DBH)

{

Int SD;

Const char ** v;

Int argc;

Const char * argv [1];

Char * select_stmt =

"SELECT NKEY, CKEY, NVAL, CVAL, DVAL from T_Sqlora_Test Where Nkey> =: 1";

Argc = 0;

Argv [Argc ] = "0";

/ * SELECT All and Display * /

IF (0> (SD = SQLO_Open (DBH, SELECT_STMT, ARGC, ARGV))))

{

Printf ("SQLO_OPEN FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

/ * SQLO_PRINT (SD); * /

While (0 == SQLO_FETCH (SD, 1))

{

v = SQLO_VALUES (SD, NULL, 1);

Printf ("% 4S% 6S% 19S% 21S% 11S / N", V [0], V [1], V [2], V [3], V [4]);

}

#ifdef close_cursor

IF (0> SQLO_CLOSE (SD))

{

Printf ("SQLO_OPEN FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

#ENDIF

Return 1;

}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------

* SELECT WITH Prepare / Execute / Fetch.

* ------------------------------------------------- ---------------------- * /

Int test_select2 (int DBH)

{

Int sth;

INT NKEY [MAX_ITERS];

Char ckey [max_iters] [6];

Double nval [max_iters];

Char Cval [MAX_ITERS] [21];

Char DVAL [MAX_ITERS] [11];

INT WC = 1;

Int status;

INT I;

Short nkeyl [max_iters];

Short ckeyl [max_iters];

Short nvall [max_iters];

Short cvall [max_iters];

Short Dvall [MAX_ITERS];

INT rows_fetched = 0;

INT rows_fetched_total = 0;

int ROWS_TO_FETCH;

INT DONE_FETCHING = 0;

Char * select_stmt =

"SELECT NKEY, CKEY, NVAL, CVAL, DVAL from T_Sqlora_Test Where Nkey> =: 1";

Printf ("Test Select Via Classic Methods / N");

/ * SELECT All and Display * /

IF (0> (st = sqlo_prepare (dbh, select_stmt)))))

{

Printf ("SQLO_PREPARE FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

/ * Bind input * /

IF (SQLO_SUCCESS! =

(SQLO_BIND_BY_NAME (Sth, ": 1", SQLOT_INT, & WC, SIZEOF (INT), NULL, 0)))))

{

Printf ("SQLO_BIND_BY_NAME FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

/ * Define Output * /

IF (SQLO_SUCCESS! =

(SQLO_DEFINE_BY_POS (Sth, 1, Sqlot_INT, NKEY, SIZEOF (Int), 0, NKEYL, 1) ||

SQLO_DEFINE_BY_POS (sth, 2, sqlot_str, ckey [0], 6, 0, ckeyl, 1) ||

SQLO_DEFINE_BY_POS (sth, 3, sqlot_flt, nval, sizeof (double), 0, nvall, 1) ||

SQLO_DEFINE_BY_POS (sth, 4, sqlot_str, cval [0], 21, 0, cvall, 1) ||

SQLO_DEFINE_BY_POS (sth, 5, sqlot_str, dval [0], 11, 0, dvall, 1)))

{

Printf ("SQLO_DEFINE_BY_POS FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

ROWS_TO_FETCH = 3;

Rows_Fetched = rows_to_fetch;

Status = SQLO_EXECUTE (sth, rows_to_fetch);

IF (Status <0)

{

Printf ("SQLO_EXECUTE FAILED:% S / N", SQLO_GETERROR (DBH));

Return (0);

}

Else IF (status == sqlo_no_data)

{

/ * Arrays WERE FILLED FULLY. Get Rowcount * /

Rows_Fetched = SQLO_PROWS (STH);

DONE_FETCHING = 1;

Printf ("Execute Fetched All% D ROWS / N", ROWS_FETCHED);

Printf ("Fetched All in One Go / N);

For (i = 0; i

{

Printf ("% 3D% 5S% 19F% 20S% 10S / N", NKEY [I], CKEY [I], NVAL [I], CVAL [I], DVAL [I]);

}

}

For (i = 0; i

{

IF (! i)

Printf ("Execute Fetched% D ROWS / N", ROWS_FETCHED);

Printf ("% 3D% 5S% 19F% 20S% 10S / N",

Nkey [i], ckey [i], nval [i], cval [i], DVAL [I]);

}

ROWS_FETCHED_TOTAL = ROWS_FETCHED;

ROWS_TO_FETCH = 4;

While (! done_fetching)

{

Rows_Fetched = rows_to_fetch;

Status = SQLO_FETCH (Sth, Rows_TO_FETCH);

IF (Status <0)

{

Printf ("SQLO_FETCH FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

IF (status == sqlo_no_data)

{

Rows_Fetched = SQLO_PROWS (STH);

IF (ROWS_FETCHED_TOTAL == ROWS_FETCHED)

{

/ * No new fetches * /

DONE_FETCHING = 1;

Rows_Fetched = 0;

}

Else

{

Rows_Fetched = rows_fetched - rows_fetched_total;

DONE_FETCHING = 1;

}

Printf ("SQLO_FETCH FETCHED LAST% D ROWS / N", ROWS_FETCHED);

}

Else IF (status == sqlo_success)

{

Printf ("SQLO_FETCH FETCHED% D ROWS / N", ROWS_FETCHED);

}

Else

{

Printf ("SQLO_FETCH FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

For (i = 0; i

{

Printf ("% 3D% 5S% 19F% 20S% 10S / N",

Nkey [i], ckey [i], nval [i], cval [i], DVAL [I]);

}

ROWS_FETCHED_TOTAL = ROWS_FETCHED;

}

#ifdef close_cursor

IF (0> SQLO_CLOSE (Sth))

{

Printf ("SQLO_CLOSE FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

#ENDIF

Return 1;

}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------

* Test_reopen

* ------------------------------------------------- ---------------------- * /

INT TEST_REOPEN (INT DBH)

{

Int sth;

Const char ** v;

Int argc;

Const char * argv [1]; char * select_stmt =

"SELECT NKEY, CKEY, NVAL, CVAL, DVAL from T_Sqlora_Test Where Nkey> =: 1";

Argc = 0;

Argv [Argc ] = "0";

/ * SELECT All and Display * /

IF (0> (st = sqlo_open (dbh, select_stmt, argc, argv))))))

{

Printf ("SQLO_OPEN FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

While (0 == SQLO_FETCH (Sth, 1))

{

v = SQLO_VALUES (Sth, Null, 1);

Printf ("% s |% 6S% 19S% 21S% 11S / N", V [0], V [1], V [2], V [3], V [4]);

}

Argv [0] = "5";

IF (SQLO_SUCCESS! = SQLO_REOPEN (Sth, Argc, Argv))

{

Printf ("SQLO_REOPEN FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

Printf ("Fetch Again / N");

While (0 == SQLO_FETCH (Sth, 1))

{

v = SQLO_VALUES (Sth, NULL, 0);

Printf ("% s |% 6S% 19S% 21S% 11S / N", V [0], V [1], V [2], V [3], V [4]);

}

#ifdef close_cursor

IF (0> SQLO_CLOSE (Sth))

{

Printf ("SQLO_OPEN FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

#ENDIF

Return 1;

}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------

* Test_PLSQL

* ------------------------------------------------- ---------------------- * /

INT Test_PLSQL (Int DBH)

{

INT IP2, OP1;

Double IP1;

CHAR OP2 [40];

Char * create_pack =

"Create or Replace Package SQLORA_TEST IS / N"

"Procedure P1 (IP1 in Number, IP2 in Number, Op1 Out Number, OP2 OUT VARCHAR); / N"

"End; / n";

Char * create_pack_body =

"Create Or Replace Package Body SQLORA_TEST IS / N"

"Procedure P1 (IP1 in Number, IP2 in Number, Op1 Out Number, Op2 Out Varchar) / N"

IS / N "

"Begin / n"

"OP1: = TO_NUMBER (IP1) IP2; / N"

"OP2: = to_CHAR (OP1); / N" "end; / n"

"End; / n";

Char * stmt =

"Begin / n"

"SQLORA_TEST.P1 (: IP1,: IP2,: OP1,: OP2); / N"

"End; / n";

Int sth;

Printf ("Testing Pl / Sql Procedure / N");

IF (SQLO_SUCCESS! = SQLO_EXEC (DBH, CREATE_PACK)

{

Printf ("SQLO_EXEC FAILED:% S / N% S / N", SQLO_GETERROR (DBH), CREATE_PACK);

Return 0;

}

Printf ("package created / n");

IF (SQLO_SUCCESS! = SQLO_EXEC (DBH, CREATE_PACK_BODY))

{

Printf ("SQLO_EXEC FAILED:% S / N% S / N", SQLO_GETERROR (DBH), CREATE_PACK_BODY);

Return 0;

}

Printf ("package body created / n");

IP1 = 1.123456789012345;

IP2 = 20;

OP1 = 0;

* OP2 = 0;

IF (0 <= (sth = SQLO_PREPARE (DBH, STMT)))))

{

IF (SQLO_SUCCESS! =

(SQLO_BIND_BY_NAME (Sth, ": IP1", SQLOT_FLT, & IP1, SIZEOF (IP1), 0, 0) ||

SQLO_BIND_BY_NAME (Sth, ": IP2", SQLOT_INT, & IP2, SIZEOF (IP2), 0, 0) ||

SQLO_BIND_BY_NAME (Sth, ": OP1", SQLOT_INT, & OP1, SIZEOF (OP1), 0, 0) ||

SQLO_BIND_BY_NAME (Sth, ": OP2", SQLOT_STR, OP2, SIZEOF (OP2), 0, 0)

))

{

Printf ("SQLO_BIND_PARAM FAILED FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

Else

{

IF (SQLO_SUCCESS! = SQLO_EXECUTE (Sth, 1))

{

Printf ("SQLO_EXECUTE FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

}

#ifdef close_cursor

IF (SQLO_SUCCESS! = SQLO_CLOSE (Sth))

{

Printf ("SQLO_CLOSE FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

#ENDIF

Printf ("IP1:% .16F, IP2:% D, OP1:% D, OP2:% S / N", IP1, IP2, OP1, OP2;

}

Else

{

Printf ("SQLO_OPEN FAILED: STATUS:% D,% S / N", Sth, SQLO_GETERROR (DBH));

Return 0;

}

Return 1;

}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------- * Test_Insert with bind by POS

* ------------------------------------------------- ---------------------- * /

INT Test_Insert (int DBH)

{

Int nkey;

Char ckey [6];

Double nval;

CHAR CVAL [21];

Char DVAL [11];

Int sth;

Char * INSERT_STMT =

"INSERT INTO T_SQLORA_TEST (NKEY, CKEY, NVAL, CVAL, DVAL) VALUES (: NKEY,: CKEY,: NVAL,: CVAL,: DVAL);

Printf ("Testing Insert (Bind By POS) / N");

IF (! CREATE_TABLE (DBH))

Return 0;

Nkey = 100;

STRCPY (CKEY, "CKEY");

NVAL = 1234567890.001;

STRCPY (CVAL, "Aaaaaaaaaaaaaaaaaaaa");

STRCPY (DVAL, "01-JUL-00");

IF (0 <= (sth = SQLO_PREPARE (DBH, INSERT_STMT))))))

{

IF (SQLO_SUCCESS! =

(SQLO_BIND_BY_POS (Sth, 1, Sqlot_INT, & NKEY, SIZEOF (Int), 0, 0) ||

SQLO_BIND_BY_POS (sth, 2, sqlot_str, ckey, 6, 0) ||

SQLO_BIND_BY_POS (sth, 3, sqlot_flt, & nval, sizeof (double), 0, 0) ||

SQLO_BIND_BY_POS (Sth, 4, Sqlot_Str, CVAL, 21, 0, 0) ||

SQLO_BIND_BY_POS (sth, 5, sqlot_str, dval, 11, 0)

))

{

Printf ("SQLO_BIND_PARAM FAILED FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

Else

{

IF (SQLO_SUCCESS! = SQLO_EXECUTE (Sth, 1))

{

Printf ("SQLO_EXECUTE FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

}

#ifdef close_cursor

IF (SQLO_SUCCESS! = SQLO_CLOSE (Sth))

{

Printf ("SQLO_CLOSE FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

#ENDIF

Do_select (dbh);

}

Else

{

Printf ("SQLO_OPEN FAILED: STATUS:% D,% S / N", Sth, SQLO_GETERROR (DBH));

Return 0;

}

Printf ("Finished Test_insert / N");

Return 1;

}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------

* Test_Array_Insert

* ------------------------------------------------- ---------------------- * / INT TEST_ARRAY_INSERT (INT DBH)

{

INT NKEY [MAX_ITERS];

Char ckey [max_iters] [6];

Double nval [max_iters];

Char Cval [MAX_ITERS] [21];

Char DVAL [MAX_ITERS] [11];

Short Nind [MAX_ITERS];

Short cind [MAX_ITERS];

Short dind [MAX_ITERS];

Int Sth, i, j;

Int status;

Char * INSERT_STMT =

"INSERT INTO T_SQLORA_TEST (NKEY, CKEY, NVAL, CVAL, DVAL) VALUES (: NKEY,: CKEY,: NVAL,: CVAL,: DVAL);

Printf ("Testing Array Insert (Bind By Name) / N");

IF (! CREATE_TABLE (DBH))

Return 0;

/ * setup bind arrays * /

For (i = 0; i

{

NKEY [I] = i 1;

Sprintf (CKEY [I], "% C", A I% 26);

NVAL [I] = 1234567890.0 I / 1000.0;

For (j = 0; j <20; j )

CVAL [I] [J] = a i% 26;

CVAL [I] [20] = /;

Sprintf (DVAL [I], "% 02D-jul-00", (i% 30) 1);

NIND [I] = 0;

CIND [I] = 0;

DIND [I] = 0;

}

IF (0 <= (sth = SQLO_PREPARE (DBH, INSERT_STMT))))))

{

IF (SQLO_SUCCESS! =

(SQLO_BIND_BY_NAME (Sth, ": Nkey", SQLOT_INT, & NKEY [0], SIZEOF (Int), NULL, 1) ||

SQLO_BIND_BY_NAME (Sth, ": CKey", SQLOT_STR, & CKEY [0], 6, NULL, 1) ||

SQLO_BIND_BY_NAME (sth, ": nval", sqlot_flt, & nval [0], sizeof (double), nind, 1) ||

SQLO_BIND_BY_NAME (Sth, ": CVAL", SQLOT_STR, & CVAL [0], 21, CIND, 1) ||

SQLO_BIND_BY_NAME (Sth, ": DVAL", SQLOT_STR, & DVAL [0], 11, DIND, 1)

))

{

Printf ("SQLO_BIND_PARAM FAILED FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

Else

{

IF (SQLO_SUCCESS! = SQLO_EXECUTE (Sth, Max_Imits))

{

Printf ("SQLO_EXECUTE FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

}

#ifdef close_cursorif (SQLO_SUCCESS! = SQLO_CLOSE (Sth))

{

Printf ("SQLO_CLOSE FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

#ENDIF

Do_select (dbh);

}

Else

{

Printf ("SQLO_OPEN FAILED: STATUS:% D,% S / N", Sth, SQLO_GETERROR (DBH));

Return 0;

}

IF (SQLO_SUCCESS! = (status = sqlo_commit (dbh))) {

Printf ("Commit Failed (% D):% S / N", Status, SQLO_GETERROR (DBH));

Return 0;

}

Return 1;

}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------

* Test_Array_INSERT2 (by POS)

* ------------------------------------------------- ---------------------- * /

INT TEST_ARRAY_INSERT2 (INT DBH)

{

INT NKEY [MAX_ITERS];

Char ckey [max_iters] [6];

Double nval [max_iters];

Char Cval [MAX_ITERS] [21];

Char DVAL [MAX_ITERS] [11];

Short Nind [MAX_ITERS];

Short cind [MAX_ITERS];

Short dind [MAX_ITERS];

INT I, J;

Int status;

Int sth;

Char * INSERT_STMT =

"INSERT INTO T_SQLORA_TEST (NKEY, CKEY, NVAL, CVAL, DVAL) VALUES (: NKEY,: CKEY,: NVAL,: CVAL,: DVAL);

Printf ("Testing Array Insert (Bind By POS) / N");

IF (! CREATE_TABLE (Sth))

Return (0);

/ * setup bind arrays * /

For (i = 0; i

{

NKEY [I] = i 1;

Sprintf (CKEY [I], "% C", A I% 26);

NVAL [I] = 1234567890.0 I / 1000.0;

For (j = 0; j <20; j )

CVAL [I] [J] = a i% 26;

CVAL [I] [20] = /;

Sprintf (DVAL [I], "% 02D-jul-00", (i% 30) 1);

NIND [I] = 0;

CIND [I] = 0;

DIND [I] = 0;

}

IF (0 <= (sth = SQLO_PREPARE (DBH, INSERT_STMT))))))

{

IF (SQLO_SUCCESS! =

(SQLO_BIND_BY_POS (Sth, 1, Sqlot_Int, & nkey [0], Sizeof (int), null, 1) ||

SQLO_BIND_BY_POS (Sth, 2, Sqlot_Str, & CKey [0], 6, NULL, 1) || SQLO_BIND_BY_POS (Sth, 3, Sqlot_flt, & nval [0], Sizeof (Double), Nind, 1) ||

SQLO_BIND_BY_POS (sth, 4, sqlot_str, & cval [0], 21, cind, 1) ||

SQLO_BIND_BY_POS (sth, 5, sqlot_str, & dval [0], 11, DIND, 1)

))

{

Printf ("SQLO_BIND_PARAM FAILED FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

Else

{

IF (SQLO_SUCCESS! = SQLO_EXECUTE (Sth, Max_Imits))

{

Printf ("SQLO_EXECUTE FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

}

#ifdef close_cursor

IF (SQLO_SUCCESS! = SQLO_CLOSE (Sth))

{

Printf ("SQLO_CLOSE FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

#ENDIF

Do_select (dbh);

}

Else

{

Printf ("SQLO_OPEN FAILED: STATUS:% D,% S / N", Sth, SQLO_GETERROR (DBH));

Return 0;

}

IF (SQLO_SUCCESS! = (status = sqlo_commit (dbh))) {

Printf ("Commit Failed (% D):% S / N", Status, SQLO_GETERROR (DBH));

Return 0;

}

Return 1;

}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------

* Test_exists

* ------------------------------------------------- ---------------------- * /

INT TEST_EXISTS (Int DBH)

{

Int status;

IF (SQLO_SUCCESS ==

(STATUS = SQLO_EXISTS (DBH, "T_SQLORA_TEST", "CKEY", "B", NULL))))

Printf ("Test_exists (1) OK / N");

Else

{

Printf ("Test_exists (1) FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

IF (SQLO_SUCCESS ==

(Status = SQLO_EXISTS (DBH, "T_Sqlora_test", "CKEY", "XXX", NULL)))

Printf ("Test_exists (2) Failed / N");

Else

{

IF (status! = sqlo_no_data)

{

Printf ("Test_exists (2) FAILED:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

Else

Printf ("Test_exists (2) OK / N");

Return 1;

}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------

* Test_count

* ------------------------------------------------- ---------------------- * /

INT Test_count (int DBH)

{

INT country;

IF ((count = SQLO_COUNT (DBH, "T_SQLORA_TEST", NULL, NULL, NULL)))

Printf ("Test_count (1) OK / N");

Else

{

Printf ("Test_count (1) Failed:% S / N", SQLO_GETERROR (DBH));

Return 0;

}

IF ((count = SQLO_COUNT (DBH, "T_SQLORA_TEST", "CKEY", "XXX", NULL)))

{

Printf ("Test_count (2) failed (count =% d) / n", count);

}

Else

{

IF (count <0)

{

Printf ("Test_Count (2) Failed (count =% D):% S / N", Count, SQLO_GETERROR (DBH));

Return 0;

}

Else

Printf ("Test_count (2) OK / N");

}

Return 1;

}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------

* INT CLEANUP

* ------------------------------------------------- ---------------------- * /

INT Cleanup (int DBH)

{

/ * ignore all errors maybe the iteren create * /

SQLO_EXEC (DBH, "DROP TABLE T_SQLORA_TEST");

SQLO_EXEC (DBH, "Drop Package Body Sqlora_test");

SQLO_EXEC (DBH, "Drop Package Sqlora_test");

Return 1;

}

/ * ================================================================================================================================================================ =========================

* main

* =========================================================================================================================================================================================== =============================== * / int main (int Argc, char * argv [])

{

Int status;

INT DBH [MAX_LOOPS];

CHAR * CSTR;

INT I;

Printf ("--------------------------------------------- ------------ / N / N ");

IF (Argc> 1)

CSTR = Argv [1];

Else

CSTR = "scott / tiger";

Status = SQLO_INIT (0);

IF (SQLO_SUCCESS! = STATUS)

{

Printf ("SQL_INIT FAILED. EXITING / N");

Exit (1);

}

For (i = 0; i

{

Status = SQLO_CONNECT (& DBH [I], CSTR);

IF (SQLO_SUCCESS == STATUS)

Printf ("Connected. DBH [I] =% D / N", DBH [I]);

Else

{

Printf ("Connect Failed with Status:% D,% S / N", Status

, SQLO_GETERROR (DBH [i]));

Exit (1);

}

IF (! Test_PLSQL (DBH [i])))

Exit (1);

IF (! test_insert (dbh [i])))

Exit (1);

IF (! Test_Array_Insert (DBH [I])) / * bind by name * /

Exit (1);

IF (! test_array_insert2 (dbh [i])) / * bind by pos * /

Exit (1);

IF (! test_exists (dbh [i])))

Exit (1);

IF (! Test_count (dbh [i])))

Exit (1);

IF (! test_reopen (dbh [i]))

Exit (1);

IF (! test_select2 (dbh [i])))

Exit (1);

Cleanup (dbh [i]);

IF (SQLO_SUCCESS! = (status = SQLO_ROLLBACK (DBH [I]))))

Printf ("Rollback Failed (% D):% S / N", STATUS, SQLO_GETERROR (DBH [i]));

}

For (i = 0; i

{

IF (SQLO_SUCCESS! = SQLO_FINISH (DBH [i]))

{

Printf ("SQL_FINISH FAILED For DBH:% D / N% S / N", DBH [i],

SQLO_GETERROR (DBH [i]));

Exit (1);

}

}

Return (0);

}

转载请注明原文地址:https://www.9cbs.com/read-8757.html

New Post(0)