RedHat AS 4.0上Oracle 10g 10.2.0.1的proc编程方法1.1主题意图1.描述在Linux RedHat AS 4.0上安装Oracle 10g 10.2.0.1的proc编程过程。1.2安装1.安装oracle时,要把程序开发包选上。1.3环境配置1.为编译方便,在$ORACLE_HOME/precomp/admin/pcscfg.cfg文件里加上如下内容,也可添加用户自己的include路径include=/home/oracle/product/10.2.0.1/db_1/precomp/public
include=/home/oracle/product/10.2.0.1/db_1/precomp/hdrs
include=/home/oracle/product/10.2.0.1/db_1/tpcc2x_2/src
include=/home/oracle/product/10.2.0.1/db_1/precomp/include
include=/home/oracle/product/10.2.0.1/db_1/oracore/include
include=/home/oracle/product/10.2.0.1/db_1/oracore/public
include=/home/oracle/product/10.2.0.1/db_1/rdbms/include
include=/home/oracle/product/10.2.0.1/db_1/rdbms/public
include=/home/oracle/product/10.2.0.1/db_1/rdbms/demo
include=/home/oracle/product/10.2.0.1/db_1/nlsrtl/include
include=/home/oracle/product/10.2.0.1/db_1/nlsrtl/public
include=/home/oracle/product/10.2.0.1/db_1/network_src/include
include=/home/oracle/product/10.2.0.1/db_1/network_src/public
include=/home/oracle/product/10.2.0.1/db_1/network/include
include=/home/oracle/product/10.2.0.1/db_1/network/public
include=/home/oracle/product/10.2.0.1/db_1/plsql/public
2.为方便所有用户访问oracle,在/etc/profile中加上如下代码,也可只在开发用户目录下的.bash_profile文件里进行添加:ORACLE_BASE=/home/oracle;export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/10.2.0.1/db_1;export ORACLE_HOME
ORACLE_SID=TEST;export ORACLE_SID
ORACLE_TERM=xterm;export ORACLE_TERM
PATH=/usr/sbin:$PATH:$HOME/bin:$ORACLE_HOME/bin;export PATH
LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib;export LD_LIBRARY_PATH
CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib;export CLASSPATH
3.用root登录把开发用户加到oinstall和dba组中:usermod -G oinstall,dba username
1.4编译make文件1.编写make文件有两种方式,一是利用ORACLE本身提供的make文件的基本模板来编写,二是自己完全自定义编写。2.利用ORACLE提的make文件模板的makefile:
include $(ORACLE_HOME)/precomp/lib/env_precomp.mk
SAMPLES=myprogram
samples:$(SAMPLES)
build:$(OBJS)
$(DEMO_PROC_BUILD_SHARED)
$(SAMPLES):
$(MAKE) -f $(MAKEFILE) OBJS=$@.o EXE=$@ build
.SUFFIXES: .pc .c .o .typ .h
pc1:
$(PCC2C)
.pc.c:
$(MAKE) -f $(MAKEFILE) PROCFLAGS="$(PROCFLAGS)" PCCSRC=$* I_SYM=include= pc1
.c.o:
$(C2O)
MAKEFILE=/tmp/1/test.mk
OTTFLAGS=$(PCCFLAGS)
PROCPLSFLAGS=sqlcheck=full userid=$(USERID)
PROCPPFLAGS=code=cpp $(CPLUS_SYS_INCLUDE)
USERID=myuser/854612
INCLUDE=$(I_SYM). $(I_SYM)$(PRECOMPHOME)public $(I_SYM)$(RDBMSHOME)public $(I_SYM)$(RDBMSHOME)demo $(I_SYM)$(PLSQLHOME)public $(I_SYM)$(NETWORKHOME)public
3.自己自定义编写的makefile例子:
ORALIBPATH=$(ORACLE_HOME)/lib
ORALIBS=-lclntsh
ORAINCLUDE=-I. -I/home/oracle/product/10.2.0.1/db_1/precomp/public -I/home/oracle/product/10.2.0.1/db_1/rdbms/public -I/home/oracle/product/10.2.0.1/db_1/rdbms/demo -I/home/oracle/product/10.2.0.1/db_1/plsql/public -I/home/oracle/product/10.2.0.1/db_1/network/public
testobj = myprogram.o
all: proc_a cco_a build_a
proc_a:
proc parse=full myprogram.pc
cco_a:
cc -c $(ORAINCLUDE) myprogram.c
build_a:
cc $(testobj) -L$(ORALIBPATH) $(ORALIBS) -o myprogram
delete:
rm *.o
rm *.cc
rm *.lis
rm tp??????
1.5编程注意事项1.包含头文件时不能用pc指定的EXEC SQL INCLUDE语法,要改用标准的#include语法。2.这个版本的编译器在编译过程会产生以tp开头后面是6个随机字符命名的0字节中间文件,不知道怎么避免。3.pc程序中执行修改数据指令后必须调用commit还能提交,否则不会真正执行更改。4.一个简单pc程序myprogram.pc:
#include
#include
int main(int argc,char *argv[])
{
char uid[20],pwd[20],ser[20];
char key[20],value[20];
strcpy(uid,"username");
strcpy(pwd,"password");
strcpy(ser,"TEST");
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd USING :ser;
if(sqlca.sqlcode!=0)
{
printf("sqlcode=%ld,sqlerr=%s\n",sqlca.sqlcode,(char *)sqlca.sqlerrm.sqlerrmc);
printf("connect error\n");
}
else printf("connect ok\n");
EXEC SQL insert into table1 values('key','value');
if(sqlca.sqlcode) {
printf("sqlcode=%ld,sqlerr=%s\n",sqlca.sqlcode,(char *)sqlca.sqlerrm.sqlerrmc);
printf("Insert Error\n");
}
else printf("Insert OK!\n");
EXEC SQL commit;
EXEC SQL select key,value into :key,:value from table where key='key';
printf("sqlcode=%ld,sqlerr=%s\n",sqlca.sqlcode,(char *)sqlca.sqlerrm.sqlerrmc);
printf("key=%s,value=%s\n",key,value);
return 0;
}