郑重声明:
本OCI(Oracle Call Interface)程序以C++编写,完全的面向对象.
具有快速,高效,可移植性好的特点,并经过严格的,长达3年以上长时间的实际使用
相信可满足绝大部分朋友的工作需要!
尤其适合应用在Linux/Unix服务器端编程使用,并支持Oracle8,9,10...等各个版本.
可支持对char,varchar,varchar2,int,float,date等类型数据进行select,update,insert操作
并支持事务的提交commit和回滚rollback.
作者:oceania 中国-四川-成都 QQ:30991118
需要的朋友可加我QQ,我把源代码全部发给你!
注意:利用oracle的instant client,结合本接口程序,即可实现不用安装oracle客户端直接访问数据库!
[OCI.h]
//文件名之所以大写是为了避免和系统的oci.h发生冲突
#ifndef _OCI_H
#define _OCI_H
#include <iostream>
#include <fstream>
#include <list>
#include <map>
#include <vector>
#include <memory>
#include <string>
#include <limits>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <signal.h>
#include <pthread.h>
#include <stdarg.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <netinet/in.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <termios.h>
#include <fcntl.h>
#include <istream>
#include "oci.h"
#include "oratypes.h"
#include "ocidfn.h"
#include "ocidem.h"
#include "ociapr.h"
const char* const NULL_STRING = "";
const int NULL_NUMBER = 0;
const int MAX_ERRMSG_LENGTH = 1024;
const int MAX_STRING_LENGTH = 1024;
class OCIException;
class OCIDatabase;
class OCIQuery;
class OCIField;
class OCIParam;
class OCIException
{
int errNo;
text errInfo[MAX_ERRMSG_LENGTH+1];
public:
OCIException(OCIError* herr);
OCIException(const char* msg);
~OCIException();
char *getErrInfo() const;
int getErrNo() const;
};
class OCIDatabase
{
char Tns[50];
char Usr[10];
char Pwd[10];
OCIEnv *hEnv;
OCIServer *hSvr;
OCIError *hDBErr;
bool active;
friend class OCIQuery;
public:
OCIDatabase();
~OCIDatabase();
void setLogin(const char* tns,const char* usr,const char* pwd) ;
int connect();
void disConnect();
};
class OCIField
{
friend class OCIQuery;
OCIQuery *ParentQuery; //指向该Field所属于的Query
ub1 StrBuf[MAX_STRING_LENGTH];//用于保存转换为字符串后的返回值
ub1 *DataBuf; //在分析字段时候获得空间max(该列的最大长度,MAX_STRING_VALUE_LENGTH), 在Destructor中释放
sb2 *DataIndicator; //在defineByPos中使用,用于在fetch时察看是否有字段值返回,字段值是否被截断;valueIsNULL,isTruncated根据此值获得结果
OCIDefine *Dfn; //用于读取列信息
char *Name; //字段名称
long Size; //数据长度
long Type; //数据类型(INT_TYPE,FLOAT_TYPE,DATE_TYPE,STRING_TYPE,ROWID_TYPE)
int Precision; //数值总长度
int Scale; //数值中小数点个数
public:
OCIField();
~OCIField();
int isNULL();
char* asString();
int asInteger();
float asFloat();
char asChar();
char* asDateTimeString(); //按HH:MM:DD HH24:MI:SS格式读取
void getDateTime(int &year, int &month, int &day, int &hour, int &minute, int &second);
};
class OCIQuery
{
friend class OCIField;
public:
OCISession *hUser;
OCIStmt *hStmt; //用于分析sql语句的handle
OCISvcCtx *hSvc; //服务
OCIError *hErr; //错误处理
sword ErrorNo; //错误号
OCIDatabase *db; //父DataBase
OCIField *FieldList; //在内部保存的所有字段信息
bool Opened; //数据集是否打开
int FieldCount; //字段个数
bool Eof; //已到最后一个记录
char *Sql;
ub2 SqlType;
unsigned Fetched;
unsigned CurrRow;
unsigned TotalRowsFetched;
bool InUse; //使用中
public:
OCIQuery();
~OCIQuery();
void setDB(OCIDatabase *oradb);
void close();//关闭Qry
void setSql(char *s);//设置sql语句
char *getSql();
void open();//执行select型sql语句
int next();//移动游标(遍历结果集时使用)
int exec();//执行select型sql语句
int commit();//提交事务
int rollBack();//回滚事务
int getAffected() {return TotalRowsFetched;};
int getFieldCount();
OCIField* field(int index);//访问某个字段
OCIField* fieldByName(char *fieldName);//访问某个字段
void getFieldsDef();
void checkError();
void sign(){InUse = true;}
void release(){InUse = false;};
};
#endif
[[makefile]
CC = g++
CFLAGS = -Wall
ORAINCLUDE = -I /home/oracle8i/include
ORALIBS = -L /home/oracle8i/lib -lclntsh
all:test
test: OCI.o test.o
$(CC) -o test *.o $(ORALIBS)
OCI.o: OCI.cpp OCI.h
$(CC) $(CFLAGS) -c OCI.cpp $(ORAINCLUDE)
test.o: test.cpp test.h
$(CC) $(CFLAGS) -c test.cpp $(ORAINCLUDE)
clean::
rm -f *.o