Linux 环境下PRO*C程序的编写简单范例
Oracle 数据库版本 10.2.1.0
Linux 版本 Red Hat Enterprise AS3
环境介绍:oracle 数据库和服务均安装到 linux 主机上
oracle 用户的环境变量设置如下
代码
[oracle@guohui6 oracle]$ cat . bash_profile
# .bash_profile
# Get the aliases and functions
if [ - f ~/. bashrc ]; then
. ~/. bashrc
fi
# User specific environment and startup programs
ORACLE_BASE =/ mydatafile2 / app / oracle; export ORACLE_BASE
ORACLE_SID = ORCL;export ORACLE_SID
# ORACLE_SID=AUXY;export ORACLE_SID
ORACLE_HOME = $ORACLE_BASE / oracle / product / 10.2 . 0 / db_1;export ORACLE_HOME
PATH = $PATH : $HOME / bin : $ORACLE_HOME / bin; export PATH
LD_LIBRARY_PATH = $ORACLE_HOME / lib :/ usr / lib :/ usr / local / lib;
export LD_LIBRARY_PATH
NLS_LANG = american_america . ZHS16GBK;export NLS_LANG
export PATH
unset USERNAME
[oracle@guohui6 oracle]$
查看oracle 用户的登录环境
代码
[oracle@guohui6 oracle]$ id
uid = 501 (oracle) gid = 501 (oinstall) groups = 501 (oinstall) , 502 (dba)
[oracle@guohui6 oracle]$ env
REMOTEHOST = 192.169 . 1.7
HOSTNAME = guohui6
SHELL =/ bin / bash
TERM = vt100
HISTSIZE = 1000
NLS_LANG = american_america . ZHS16GBK
QTDIR =/ usr / lib / qt - 3.1
OLDPWD =/ home / oracle / backuprestor101 / proc_code
USER = oracle
LD_LIBRARY_PATH =/ mydatafile2 / app / oracle / oracle / product / 10.2 . 0 / db_1 / lib :/ usr / lib :/ usr / local / lib
LS_COLORS = no = 00 : fi = 00 : di = 01 ; 34 : ln = 01 ; 36 : pi = 40 ; 33 : so = 01 ; 35 : bd = 40 ; 33 ; 01 : cd = 40 ; 33 ; 01 : or = 01 ; 05 ; 37 ; 41 : mi = 01 ; 05 ; 37 ; 41 : ex = 01 ; 32 :*. cmd = 01 ; 32 :*. exe = 01 ; 32 :*. com = 01 ; 32 :*. btm = 01 ; 32 :*. bat = 01 ; 32 :*. sh = 01 ; 32 :*. csh = 01 ; 32 :*. tar = 01 ; 31 :*. tgz = 01 ; 31 :*. arj = 01 ; 31 :*. taz = 01 ; 31 :*. lzh = 01 ; 31 :*. zip = 01 ; 31 :*. z = 01 ; 31 :*. Z = 01 ; 31 :*. gz = 01 ; 31 :*. bz2 = 01 ; 31 :*. bz = 01 ; 31 :*. tz = 01 ; 31 :*. rpm = 01 ; 31 :*. cpio = 01 ; 31 :*. jpg = 01 ; 35 :*. gif = 01 ; 35 :*. bmp = 01 ; 35 :*. xbm = 01 ; 35 :*. xpm = 01 ; 35 :*. png = 01 ; 35 :*. tif = 01 ; 35 :
ORACLE_SID = ORCL
ORACLE_BASE =/ mydatafile2 / app / oracle
MAIL =/ var / spool / mail / oracle
PATH =/ usr / kerberos / bin :/ usr / local / bin :/ bin :/ usr / bin :/ usr / X11R6 / bin :/ home / oracle / bin :/ mydatafile2 / app / oracle / oracle / product / 10.2 . 0 / db_1 / bin
INPUTRC =/ etc / inputrc
PWD =/ home / oracle
LANG = en_US . UTF - 8
LAMHELPFILE =/ etc / lam / lam - helpfile
SSH_ASKPASS =/ usr / libexec / openssh / gnome - ssh - askpass
SHLVL = 1
HOME =/ home / oracle
LOGNAME = oracle
LESSOPEN =|/ usr / bin / lesspipe . sh % s
ORACLE_HOME =/ mydatafile2 / app / oracle / oracle / product / 10.2 . 0 / db_1
G_BROKEN_FILENAMES = 1
_ =/ bin / env
[oracle@guohui6 oracle]$
注意:LD_LIBRARY_PATH 变量的设置,PATH环境变量的设置
代码
PATH =/ usr / kerberos / bin :/ usr / local / bin :/ bin :/ usr / bin :/ usr / X11R6 / bin :/ home / oracle / bin :/ mydatafile2 / app / oracle / oracle / product / 10.2 . 0 / db_1 / bin
LD_LIBRARY_PATH =/ mydatafile2 / app / oracle / oracle / product / 10.2 . 0 / db_1 / lib :/ usr / lib :/ usr / local / lib
如果设置得不对,将无法正确的调用 proc 预编译工具,也无法正确的链接 proc 预编译需要使用的头文件。
确保数据库连接和tnsnames.ora,listener.ora 文件的正确配置。(这里就不再多说了!)
代码
[oracle@guohui6 oracle]$ sqlplus scott / tiger@orcl
SQL * Plus : Release 10.2 . 0.1 . 0 - Production on Mon Jul 5 01 : 16 : 36 2010
Copyright (c) 1982 , 2005 , Oracle . All rights reserved .
Connected to :
Oracle Database 10g Enterprise Edition Release 10.2 . 0.1 . 0 - Production
With the Partitioning , OLAP and Data Mining options
SQL >
我们将用 scott用户对数据库进行连接并查找表里面的数据。
代码 main.pc 如下
代码
#include < stdio.h >
#include < string .h >
#include < stdlib.h >
#include < sqlca.h >
int main( int argc, char * argv[])
{
EXEC SQL BEGIN DECLARE SECTION;
char * uid = " scott/tiger@ORCL " ;
long userid = 0 ;
char username[ 51 ] = "" ;
EXEC SQL VAR username IS STRING( 51 );
EXEC SQL END DECLARE SECTION;
EXEC SQL CONNECT :uid;
if (sqlca.sqlcode == 0 )
printf( " connect sucess!\n " );
else
printf( " connect fail!\n " );
EXEC SQL DECLARE cur_user CURSOR FOR SELECT DEPTNO, DNAME FROM DEPT;
EXEC SQL OPEN cur_user;
while ( 1 ){
userid = 0 ;
strcpy(username, "" );
EXEC SQL FETCH cur_user INTO :userid, :username;
if ( sqlca.sqlcode == 1403 ) break ;
printf( " userid=%ld,username=%s\n " ,userid,username);
}
EXEC SQL CLOSE cur_user;
// EXEC SQL COMMIT WORK RELEASE;
EXEC SQL ROLLBACK WORK RELEASE;
return 0 ;
}
这里用 scott 用户对数据库发起连接,并查询 dept 表中的数据并打印出来。
第一步:预编译
代码
[oracle@guohui6 proc_code]$ proc main . pc
Pro * C / C ++: Release 10.2 . 0.1 . 0 - Production on Mon Jul 5 01 : 27 : 37 2010
Copyright (c) 1982 , 2005 , Oracle . All rights reserved .
System default option values taken from : / mydatafile2 / app / oracle / oracle / product / 10.2 . 0 / db_1 / precomp / admin / pcscfg . cfg
[oracle@guohui6 proc_code]$
第二步:编译预编译生成的文件 main.c
[oracle@guohui6 proc_code]$ gcc - o tespproc main . c - I $ORACLE_HOME / precomp / public - L $ORACLE_HOME / lib - lclntsh
[oracle@guohui6 proc_code]$
测试执行
代码
[oracle@guohui6 proc_code]$ ./ tespproc
connect sucess !
userid = 50 , username = SUPPORT
userid = 10 , username = ACCOUNTING
userid = 20 , username = RESEARCH
userid = 30 , username = SALES
userid = 40 , username = OPERATIONS
[oracle@guohui6 proc_code]$
执行成功
备注:
编译错误oracle proc PCC-S-02015:
运行proc main.pc生成c文件时可能会产生这个错误,请检查文件$(ORACLE_HOME)/precomp/admin/pcscfg.cfg,在/usr/lib/下查找未找到的"stddef.h"文件,将找到的路径添加到pcscfg.cfg。