Oracle數據庫編程2(OCI方式)

//============================================================================
// Name        : CExercise.cpp
// Author      : Haier
// Version     : 0.1
// Copyright   : Your copyright notice
// Description : Connect Oracle in C++, Ansi-style
//============================================================================

#include <oci.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <ociapr.h>
#include <ocidem.h>
using namespace std;

Lda_Def lda;
cda_def cda;

dvoid oci_error(void)
{
    text msg[600];
    sword rv;
    //取錯誤信息
    rv=oerhms(&lda,cda.rc,msg,600);
    //顯示錯誤碼和錯誤信息
    printf("\n\n%.*s",rv,msg);
    //顯示發生錯誤的oci函數
    printf("processing oci function %s\n",oci_func_tab[cda.fc]);
    //關閉光標
    if(oclose(&cda))
        printf("error closing cursor!\n");
    if(ologof(&lda))
        printf("error logging off!\n");

    exit(1);
}


int main()
{
	char szDbUser[]="so1@KFCS";
	char szPassword[] ="1qaz!QAZ";
	char pc_msg[256];
	char sql[256] = "update so1.ins_prod_a set state='1' where bill_id='15238075968'";

	if(olon( &lda,(OraText*)szDbUser, -1, (OraText*)szPassword, -1, 0))
	{
		oci_error();
	}

	cout<<"Connect to Oracle DB Successed!"<<endl;

	if(oopen(&cda,&lda,(OraText*)0,-1,-1,(OraText*)0,-1))
	{
		oci_error();
	}

	cout<<"Open Curse Successed!"<<endl;


	if(oparse(&cda,(OraText*)sql,-1,0,2))
	{
		oci_error();
	}

	cout<<"Parse Sql Successed!"<<endl;

	if(oexn( &cda, 1, 0 ))
	{
		oci_error();
	}

	cout<<"exec Sql Successed!"<<endl;

	if(oparse(&cda,(text*)"commit",-1,0,2))
	{
		oci_error();
	}

	cout<<"Commit Successed!"<<endl;

	if(oclose(&cda))
	{
		printf("error closing cursor!/n");
		exit(1);
	}

	//結束事務,退出oracle
	if(ologof(&lda))
	{
		printf("error logging off!/n");
		exit(1);
	}

	return 0;
}

運行示例:

Connect to Oracle DB Successed!
Open Curse Successed!
Parse Sql Successed!
exec Sql Successed!
Commit Successed!


發佈了56 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章