pro*c除了oracle-XE外都是在oracle安装的时候默认安装的。
一.新建一个pc文件demo01.pc内容如下:
#include
#include
void main()
{
char
username[10],password[10],server[10];
char name[11];
int lid;
printf("Enter the username: ");
gets(username);
printf("Enter the password: ");
gets(password);
printf("Enter the servername: ");
gets(server);
EXEC SQL CONNECT :username IDENTIFIED BY
:password USING :server;
printf("Enter the employee name: ");
gets(name);
EXEC SQL SELECT lid,lname INTO :lid,:name
FROM yofee_test
WHERE
UPPER(lname)=UPPER(:name);
printf("id: %d,name: %s
",lid,name);
EXEC SQL COMMIT RELEASE;
}
二. 尝试用proc编译文件并生成c语言程序
proc demo01.pc
一般来讲会报错说是找不到相关的头文件信息,然后就要去查找需要的这个头文件是在哪个路径下,找到后把这个路径加到proc的配置文件里即可。
[root@localhost codes]# find / -name "stddef.h" -print
/usr/src/kernels/2.6.9-89.EL-i686/include/linux/stddef.h
/usr/lib/gcc/i386-redhat-linux/3.4.3/include/stddef.h
/usr/include/linux/stddef.h
[root@localhost codes]# cd
/u01/oracle/product/11.2.0/dbhome_1/precomp/admin/
[root@localhost admin]# vi pcscfg.cfg
//加入找到的这个include路径到里面去再重新proc编辑就没有问题了。
三. 现在即可看到已经生成了一个新的c语言源代码,通过gcc编译这个文件
[oracle@localhost codes]$ gcc demo01.c -o demo01 -I
/u01/oracle/product/11.2.0/dbhome_1/precomp/public -L
/u01/oracle/product/11.2.0/dbhome_1/lib/ -l clntsh
四.确保监听器正常运行以便用户可以正常登陆进oracle,没有运行就打开他。
lsnrctl start 要打开监听器才能确保用户能够连进去
五. 编译成功后就可以执行这个文件,一切正常!
[oracle@localhost codes]$ ./demo01
Enter the username: yofee
Enter the password: yofee
Enter the servername: etl
Enter the employee name: yuki
id: 2,name: yuki
附(连接数据库并用游标遍历查询表中所有数据并打印出来)
#include
#include
#include
#include
exec sql begin declare section;
char *uid="yofee/yofee@etl";
int pid;
int mid;
char subject[20]="subject_one";
char login_id[15]=" ";
int score;
exec sql end declare section;
int main()
{
exec sql connect :uid;
if(sqlca.sqlcode==0)
{
printf("connect
success...
");
exec sql declare cur_vote
cursor for
select
pid,subject,mid,login_id from tmp_yofee_vote;
exec sql open
cur_vote;
while(1)
{
pid=0;
mid=0;
score=0;
exec
sql fetch cur_vote into :pid , :subject , :mid, :login_id;
if(sqlca.sqlcode == 1403)
{break;}
else
{
printf ("product_id=%d,
product_name=%s,member_id=%d,member_name=%s,score=%d
",pid,subject,mid,login_id,score);
}
}
exec sql close cur_vote;
EXEC SQL COMMIT RELEASE;
}
else
printf("%s",sqlca.sqlerrm.sqlerrmc);
}