Linux下編譯Proc程式,Linux編譯Proc程式

來源:互聯網
上載者:User

Linux下編譯Proc程式,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/includesys_include=/usr/include/oracle/11.2/client64

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

sys_include=/usr/include/x86_64-linux-gnusys_include=/usr/lib/gcc/x86_64-linux-gnu/4.8/includesys_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>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;}

相關文章

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.