Oracle oci's handling of null

Source: Internet
Author: User
Tags recode

Processing null in oci must be completed through Indicator.

Oracle Database 11g Enterprise Edition Release 11.1.0.6.0

# Include <stdlib. h>
# Include <stdio. h>
# Include <string. h>
# Include "include \ oci. h"
# Include "include \ ociextp. h"

 

# Define ERRGOTO (Recode) do \
{\
If (Recode! = 0 )\
{\
Goto ERR ;\
}\
} While (0)

 

Void main ()
{

Int a = 0;
OCIDefine * defhp1 = 0;
OCIDefine * defhp2 = 0;
Int blength = 40;
Char B [40];
Sb2 indicator;

Text * select_ SQL = (text *) "select a, B from teststmt2 ";

Dvoid * tmp;
OCIEnv * envhp;
OCIServer * srvhp;
OCIError * errhp;
OCISvcCtx * svchp;
OCISession * usrhp;
OCIStmt * effecthp;


Char serName1 [30] = "10.0.4.161: 1521/orcl ";
Char userName1 [30] = "cxy ";
Char pwd1 [30] = "cxy ";

//! If no data exists, use this test case to create a data table.
// Test_BindArrayOfStruct ();

Memset (B, 0, blength );

//! Initialize Environment Change handle
ERRGOTO (OCIEnvCreate (& (envhp), OCI_DEFAULT,
NULL, 0, NULL ));

//! Initialization error handle
ERRGOTO (OCIHandleAlloc (dvoid *) envhp, (dvoid **) & errhp, OCI_HTYPE_ERROR, 64, (dvoid **) & tmp ));

//! Allocate service context handle and Service handle
ERRGOTO (OCIHandleAlloc (dvoid *) envhp, (dvoid **) & srvhp, OCI_HTYPE_SERVER, 64, (dvoid **) & tmp ));
ERRGOTO (OCIHandleAlloc (dvoid *) envhp, (dvoid **) & svchp, OCI_HTYPE_SVCCTX, 64, (dvoid **) & tmp ));

//! Initialize server handle
ERRGOTO (OCIServerAttach (srvhp, errhp, (text *) serName1, (sb4) strlen (serName1), (ub4) OCI_DEFAULT ));

//! /Connect the server handle to the service context handle
ERRGOTO (OCIAttrSet (dvoid *) svchp, OCI_HTYPE_SVCCTX, (dvoid *) srvhp, (ub4) 0, OCI_ATTR_SERVER, errhp ));

//! Allocate and set the session handle, and fill in the username and password
ERRGOTO (OCIHandleAlloc (dvoid *) envhp, (dvoid **) & usrhp, OCI_HTYPE_SESSION, 0, (dvoid **) 0 ));
ERRGOTO (OCIAttrSet (dvoid *) usrhp, OCI_HTYPE_SESSION, (dvoid *) userName1, (ub4) strlen (userName1), OCI_ATTR_USERNAME, errhp ));
ERRGOTO (OCIAttrSet (dvoid *) usrhp, OCI_HTYPE_SESSION, (dvoid *) pwd1, (ub4) strlen (pwd1), OCI_ATTR_PASSWORD, errhp ));

//! Establish a session
ERRGOTO (OCISessionBegin (svchp, errhp, usrhp, OCI_CRED_RDBMS, OCI_DEFAULT ));

//! Connect the session handle to the service context handle
ERRGOTO (OCIAttrSet (dvoid *) svchp, OCI_HTYPE_SVCCTX, (dvoid *) usrhp, (ub4) 0, OCI_ATTR_SESSION, errhp ));

//! Allocate statement handle
ERRGOTO (OCIHandleAlloc (dvoid *) envhp, (dvoid **) & effecthp, OCI_HTYPE_STMT, 0, 0 ));

 


//! Start query and obtain results
Text * delete_table = (text *) "delete from teststmt2 ";
ERRGOTO (OCIStmtPrepare (effecthp, errhp, (text *) delete_table, strlen (char *) delete_table), OCI_NTV_SYNTAX, OCI_DEFAULT ));
ERRGOTO (OCIStmtExecute (svchp, rjthp, errhp, OCI_DEFAULT ));


Int aa = 8;
Char bb [] = "bbbbbb ";
Indicator =-1;
Text * insert_ SQL = (text *) "insert into teststmt2 (a, B) values (: 1,: 2 )";
OCIBind * bindhp = 0;
//! Prepare data and bind Parameters
ERRGOTO (OCIStmtPrepare (effecthp, errhp, (text *) insert_ SQL, strlen (char *) insert_ SQL), OCI_NTV_SYNTAX, OCI_DEFAULT ));
ERRGOTO (OCIBindByPos (rjthp, & bindhp, errhp, 1, (dvoid *) & aa, 4, SQLT_INT, (void *) & indicator, NULL, NULL, 0, 0, 0 ));
// OCIBindArrayOfStruct (bindhp, errhp, sizeof (int), 0, 0 );

ERRGOTO (OCIBindByPos (rjthp, & bindhp, errhp, 2, (dvoid *) & bb, 6, SQLT_CHR, NULL, 0, 0 ));
// OCIBindArrayOfStruct (bindhp, errhp, 2, 0, 0 );

//! Execution statement
ERRGOTO (OCIStmtExecute (svchp, rjthp, errhp, 0 ));
 
Indicator =-1;

//! Start query and obtain results
ERRGOTO (OCIStmtPrepare (effecthp, errhp, (text *) select_ SQL, strlen (char *) select_ SQL), OCI_NTV_SYNTAX, OCI_DEFAULT ));
//! Bind to a column
ERRGOTO (OCIDefineByPos (effecthp, & defhp1, errhp, 1, & a, sizeof (a), SQLT_INT, (void *) & indicator, OCI_DEFAULT ));
// ERRGOTO (OCIDefineArrayOfStruct (defhp1, errhp, sizeof (int), 0, 4, 0 ));
//! Bind with two columns

Indicator =-2;
ERRGOTO (OCIDefineByPos (effecthp, & defhp2, errhp, 2, & B, 20, SQLT_CHR, (void *) & indicator, 0, 0, OCI_DEFAULT ));
// ERRGOTO (OCIDefineArrayOfStruct (defhp2, errhp, 2, 0, 2, 0 ));
//! Execution statement
ERRGOTO (OCIStmtExecute (svchp, rjthp, errhp, OCI_DEFAULT ));
// ERRGOTO (OCIStmtFetch (%thp, errhp, 1, OCI_FETCH_NEXT, 0 ));

Printf ("% d, % s, % d", a, B, indicator );

ERRGOTO (OCITransCommit (svchp, errhp, 0 ));

 
//! Release resources
 
// ERRGOTO (OCIHandleFree (srvhp, OCI_HTYPE_SERVER ));
// ERRGOTO (OCIHandleFree (errhp, OCI_HTYPE_ERROR ));
// ERRGOTO (OCIHandleFree (envhp, OCI_HTYPE_ENV ));

 

Return;
ERR:
Sb4 errcod = 0;
OraText msgerr [200] = "";
OraText msgstat [200] = "";
OCIErrorGet (errhp, 1, msgstat, & errcod, Ms Gerr, 200, OCI_HTYPE_ERROR );
Printf (char *) msgerr );
Return;

 

}

  • 1
  • 2
  • Next Page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.