Linux下编译Proc程序

需要在linux写简单的Proc程序,资料很少,完成后做简单总结。
中间参考过http://blog.csdn.net/gaogao303/article/details/17303453中的很多内容,表示感谢。

1、 从oracle网站下载以下安装包
注意根据OS版本进行选择64位或者32位。其中sqlplus用于连接测试,可以不装。
oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-precomp-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm

2、 按照以下顺序安装
先装basic,其他顺序没有限制。
oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-precomp-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm

如果要ubuntu下安装,要现用alien转成deb后安装。
可能需要先下载alien

apt-get install alien

然后:

alien -i oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm  
alien -i oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm  
alien -i oracle-instantclient11.2-precomp-11.2.0.4.0-1.x86_64.rpm  
alien -i oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm

3、 设定环境变量
默认安装路径是/usr/lib/oracle,此时如下设定

export ORACLE_HOME=/usr/lib/oracle/11.2/client64/
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib

另外,
ldconfig /usr/lib/oracle/11.2/client64/lib一下已确保动态库路径被更新。

4、 测试
1) 执行proc xx.pc 进行测试。
2) 执行sqlplus64 user/pass@host:port/DBNAME进行连接测试
xx.pc可用以下代码

5、 Proc设定
环境变量设定好后,proc会默认到ORACLEHOME/precomp/admin ORACLE_HOME/lib下面复制一份即可。一般需要追加配置的是linux系统、gcc、和oracle的头文件路径。

1) Centos6.5下追加以下配置通过:

sys_include=/usr/lib/gcc/i686-redhat-linux/4.4.7/include
sys_include=/usr/include/oracle/11.2/client64

2) ubuntu14.04下追加以下配置通过

sys_include=/usr/include/x86_64-linux-gnu
sys_include=/usr/lib/gcc/x86_64-linux-gnu/4.8/include
sys_include=/usr/include/oracle/11.2/client64

pc文件可参照
http://docs.oracle.com/cd/B28359_01/appdev.111/b28427/toc.htm

#include <stdio.h>
#include <string.h>
#include <sqlca.h>
#include <oraca.h>

#include "db.h"

EXEC ORACLE OPTION(ORACA=YES);
int db_connect(const char* connstr)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char uid[256] = {0};
    EXEC SQL END DECLARE SECTION;

    EXEC SQL WHENEVER SQLERROR GOTO ORACLE_ERROR;
    snprintf(uid, sizeof(uid), "%s", connstr);

    // Try to connect to oracle
    EXEC SQL CONNECT :uid;

    printf("Connect to [%s] successful\n", uid);
    return 0;

ORACLE_ERROR:

    EXEC SQL WHENEVER SQLERROR CONTINUE;
    printf("Connect to [%s] failed\n", uid);
    printf("Failed code(%d), message(%s)\n",
            sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);

    return -1;
}

int db_disconnect(void)
{
    EXEC SQL WHENEVER SQLERROR GOTO ORACLE_ERROR;
    EXEC SQL ROLLBACK WORK RELEASE;
    /*
    printf("Disconnect successful\n");
    */
    return 0;

ORACLE_ERROR:
    EXEC SQL WHENEVER SQLERROR CONTINUE;
    return -1;
}

int db_query(const char* name, const char* pass)
{

    EXEC SQL BEGIN DECLARE SECTION;
    char username[128] = {0};
    varchar passwords[128];
    int types = 0;
    EXEC SQL END DECLARE SECTION;

    EXEC SQL WHENEVER SQLERROR GOTO ORACLE_ERROR;
    memset(&passwords, 0, sizeof(passwords));
    snprintf(username, sizeof(username), "%s", name);

    EXEC SQL SELECT PASSWORD, TYPE
            INTO :passwords, :types
    FROM USER
            WHERE NAME = :username;

    printf("----\n");
    printf("USER_NAME = %s\nPASSWORD=%s\nUSER_TYPE=%d\n", username, (char*)passwords.arr, types[0]);
    printf("----\n");

    return 0;

ORACLE_ERROR:
    EXEC SQL WHENEVER SQLERROR CONTINUE;
    return -1;
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。