需要在linux寫簡單的Proc程序,資料很少,完成後做簡單總結。
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會默認到
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
#include
#include
#include
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;
}