AIX下Oracle数据库访问助你走向成功(aix访问oracle)
X下Oracle数据库访问助你走向成功
在现今信息化的时代,数据库无疑是所有企业的重要基础设施之一,而Oracle数据库更成为了数据库市场中的领头羊。而在X操作系统下,如何访问Oracle数据库呢?本篇文章将为大家介绍X下Oracle数据库访问的关键点,并提供相应的代码示例,助你走向成功。
一、安装Oracle即客户端
在X操作系统中,我们需要先安装Oracle客户端,以便能够访问Oracle数据库。通常情况下,我们可以使用Oracle Instant Client这一轻量级的客户端,安装步骤如下:
1. 下载对应版本的Oracle Instant Client,解压到指定目录。
2. 为Oracle Instant Client创建软链接:
ln -s libocci.so.11.1 libocci.so
ln -s libclntsh.so.11.1 libclntsh.so
3. 修改环境变量,将Oracle Instant Client所在目录加入到环境变量LD_LIBRARY_PATH中:
export LD_LIBRARY_PATH=/path/to/instantclient:$LD_LIBRARY_PATH
二、连接Oracle数据库
连接Oracle数据库的方法有很多,其中最常用的是使用OCI方法进行连接。OCI(Oracle Call Interface)是Oracle提供的一套C语言接口,可以用于开发应用程序,并且支持多线程和多个客户端连接。以下是连接Oracle数据库的完整示例代码:
“`c
#include
#include
#include
int mn()
{
OCIEnv * envhp;
OCIError * errhp;
OCIStmt * stmthp;
OCIServer * srvhp;
OCISession * sesnhp;
OCIString * str;
OCISvcCtx * svcCtx;
char * host = “127.0.0.1”; // Oracle服务器IP
char * user = “scott”; // Oracle用户名
char * password = “tiger”; // Oracle密码
char * dbname = “orcl”; // Oracle数据库名
// 初始化OCI环境
if (OCIEnvCreate(&envhp, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL)) {
printf(“OCIEnvCreate() fled\n”);
return -1;
}
// 创建OCI Error handle
if (OCIHandleAlloc(envhp, (void**)&errhp, OCI_HTYPE_ERROR, 0, NULL)) {
printf(“OCIHandleAlloc() fled\n”);
return -1;
}
// 创建OCI Server handle
if (OCIHandleAlloc(envhp, (void**)&srvhp, OCI_HTYPE_SERVER, 0, NULL)) {
printf(“OCIHandleAlloc() fled\n”);
return -1;
}
// 创建OCI Service handle
if (OCIHandleAlloc(envhp, (void**)&svcCtx, OCI_HTYPE_SVCCTX, 0, NULL)) {
printf(“OCIHandleAlloc() fled\n”);
return -1;
}
// 创建OCI String
if (OCIHandleAlloc(envhp, (void**)&str, OCI_HTYPE_STRING, 0, NULL)) {
printf(“OCIHandleAlloc() fled\n”);
return -1;
}
// 设置OCI Server handle属性
if (OCIServerAttach(srvhp, errhp, (const OraText*)dbname, strlen(dbname), OCI_DEFAULT)) {
printf(“OCIServerAttach() fled\n”);
return -1;
}
// 设置OCI Service handle属性
if (OCIAttrSet((void*)svcCtx, OCI_HTYPE_SVCCTX, (void*)srvhp, (ub4)0, OCI_ATTR_SERVER, errhp)) {
printf(“OCIAttrSet() fled\n”);
return -1;
}
// 创建OCI Session handle
if (OCIHandleAlloc(envhp, (void**)&sesnhp, OCI_HTYPE_SESSION, 0, NULL)) {
printf(“OCIHandleAlloc() fled\n”);
return -1;
}
// 设置OCI Session handle属性
if (OCIAttrSet((void*)sesnhp, OCI_HTYPE_SESSION, (void*)user, (ub4)strlen(user), OCI_ATTR_USERNAME, errhp)) {
printf(“OCIAttrSet() fled\n”);
return -1;
}
if (OCIAttrSet((void*)sesnhp, OCI_HTYPE_SESSION, (void*)password, (ub4)strlen(password), OCI_ATTR_PASSWORD, errhp)) {
printf(“OCIAttrSet() fled\n”);
return -1;
}
// 建立OCI Session连接
if (OCISessionBegin(svcCtx, errhp, sesnhp, OCI_CRED_RDBMS, OCI_DEFAULT)) {
printf(“OCISessionBegin() fled\n”);
return -1;
}
// 设置OCI Service handle属性
if (OCIAttrSet((void*)svcCtx, OCI_HTYPE_SVCCTX, (void*)sesnhp, (ub4)0, OCI_ATTR_SESSION, errhp)) {
printf(“OCIAttrSet() fled\n”);
return -1;
}
// 创建OCI Statement handle
if (OCIHandleAlloc(envhp, (void**)&stmthp, OCI_HTYPE_STMT, 0, NULL)) {
printf(“OCIHandleAlloc() fled\n”);
return -1;
}
// 执行SQL语句
if (OCIStmtPrepare(stmthp, errhp, (const OraText*)”SELECT * FROM emp”, strlen(“SELECT * FROM emp”), OCI_NTV_SYNTAX, OCI_DEFAULT)) {
printf(“OCIStmtPrepare() fled\n”);
return -1;
}
if (OCIStmtExecute(svcCtx, stmthp, errhp, 0, 0, NULL, NULL, OCI_DEFAULT)) {
printf(“OCIStmtExecute() fled\n”);
return -1;
}
// 解析SQL语句
OCIParam * param;
char * colval;
ub4 colsize;
ub2 coltype;
ub4 colind;
ub4 ncol;
OCIDefine *defhp;
if (OCIStmtExecute(svcCtx, stmthp, errhp, 0, 0, NULL, NULL, OCI_DESCRIBE_ONLY)) {
printf(“OCIStmtExecute() fled\n”);
return -1;
}
if (OCIAttrGet((dvoid *) stmthp, OCI_HTYPE_STMT, (dvoid *)&ncol, NULL, OCI_ATTR_PARAM_COUNT, errhp)) {
printf(“OCIAttrGet() fled\n”);
return -1;
}
for (int i = 1; i
if (OCIParamGet(stmthp, OCI_HTYPE_STMT, errhp, (dvoid **)¶m, i)) {
printf(“OCIParamGet() fled\n”);
return -1;
}
if (OCIAttrGet(param, OCI_DTYPE_PARAM, (dvoid *)&colval, (ub4 *)&colsize, OCI_ATTR_NAME, errhp)) {
printf(“OCIAttrGet() fled\n”);
return -1;
}
printf(“%s\t”, colval);
if (OCIAttrGet(param, OCI_DTYPE_PARAM, (dvoid *)&coltype, NULL, OCI_ATTR_DATA_TYPE, errhp)) {
printf(“OCIAttrGet() fled\n”);
return -1;
}
if (OCIAttrGet(param, OCI_DTYPE_PARAM, (dvoid *)&colsize, NULL, OCI_ATTR_DATA_SIZE, errhp)) {
printf(“OCIAttrGet() fled\n”);
return -1;
}
if (OCIAttrGet(param, OCI_DTYPE_PARAM, (dvoid *)&colind, NULL, OCI_ATTR_DATA_LENGTH, errhp)) {
printf(“OCIAttrGet() fled\n”);
return -1;
}
if (OCIAttrSet(stmthp, OCI_HTYPE_STMT, (void*)&defhp, (ub4)sizeof(defhp), OCI_ATTR_DEFHP, errhp)) {
printf(“OCIAttrSet() fled\n”);
return -1;
}
void *data = (void*) malloc(colsize);
if (OCIAttrSet(defhp, OCI_HTYPE_DEFINE, data, colsize, OCI_ATTR_DATA_PTR, errhp)) {
printf(“OCIAttrSet() fled\n”);
return -1;
}
if (OCIAttrSet(defhp, OCI_HTYPE_DEFINE, (void *)&colind, (ub4)sizeof(colind), OCI_ATTR_INDICATOR, errhp)){
printf(“OCIAttrSet() fled\n”);
return -1;
}
switch (coltype) {
case SQLT_CHR:
if (OCIStmtBindByName(stmthp, &defhp, errhp, (const OraText *)colval, strlen(colval),
(void*)data, colsize, SQLT_STR, 0, NULL, NULL, 0, NULL