阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

使用PRO*C链接Oracle数据库步骤及问题

135次阅读
没有评论

共计 3024 个字符,预计需要花费 8 分钟才能阅读完成。

PRO* C 是 Oracle 提供的应用程序专用开发工具,它以 C 语言为宿主语言,能在 C 程序中嵌入 SQL 语句,进行数据库操作。

下载 Client-server 安装包

官方下载路径:http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html

使用 PRO* C 链接 Oracle 数据库步骤及问题

建议下载 RPM 包,方便安装。

注明:Base 下的 Basic Package 和 Basic Light Package 是相同的安装包,下载一个就行。

下载 Precompiler:

官方下载路径:http://www.oracle.com/technetwork/topics/precomp-112010-084940.html

安装 RPM 包

以安装 PROC 为例:

[linuxidc@ Oracle~]$ rpm -ivh oracle-instantclient18.3-precomp-18.3.0.0.0-1.x86_64.rpm
准备中 …                          ################################# [100%]
正在升级 / 安装 …
  1:oracle-instantclient18.3-precomp-################################# [100%]

安装完成后,执行 proc - v 出现错误:

[linuxidc@ Oracle~]$ proc -v
proc: error while loading shared libraries: libclntsh.so.18.1: cannot open shared object file: No such file or directory

解决办法:

[linuxidc@ Oracle~]$ vi /etc/ld.so.conf

在末尾增加一行 /usr/lib/oracle/18.3/client64/lib/
执行

[linuxidc@ Oracle~]$ ldconfig
[linuxidc@ Oracle~]$ proc -v

Pro*C/C++: Release 18.0.0.0.0 – Production on Thu Aug 2 10:55:09 2018
Version 18.3.0.0.0

编译 PC 文件

创建 PC 文件

[linuxidc@ Oracle~]$ touch connect.pc
[linuxidc@ Oracle~]$ vim connect.pc

录入代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sqlca.h>

void main()
{
  char username[10], password[10], server[10];
  char name[11], title[10];
  float salary;
  printf(“input name:”);
  gets(username);
  printf(“input pass:”);
  gets(password);
  printf(“input server:”);
  gets(server);
  EXEC SQL CONNECT :username IDENTIFIED BY :password USING :server;
  printf(“input empoyee name:”);
  gets(name);
  EXEC SQL SELECT sal, job INTO :salary, :title FROM emp WHERE UPPER(ename) = UPPER(:name);
  printf(“title:%s, salary:%6.2f\n”, title, salary);
  EXEC SQL COMMIT RELEASE;
}

PROC 编译

[linuxidc@ Oracle~]$ proc iname=connect.pc parse=full include=/usr/local/include/ include=/usr/include/
Pro*C/C++: Release 18.0.0.0.0 – Production on Thu Aug 2 09:51:53 2018
Version 18.3.0.0.0

Copyright (c) 1982, 2018, Oracle and/or its affiliates.  All rights reserved.

System default option values taken from: /usr/lib/oracle/18.3/client64/lib/precomp/admin/pcscfg.cfg

生成文件

[linuxidc@ Oracle~]$ ls
connect.pc  connect.c  connect.lis

GCC 编译

直接 GCC 编译,发生错误:

[root@vio062033 Oracle]# gcc -o connect connect.c
connect.c: 在函数‘main’中:
connect.c:155:3: 警告:不建议使用‘gets’(声明于 /usr/include/stdio.h:638) [-Wdeprecated-declarations]
  gets(username);
  ^
connect.c:157:3: 警告:不建议使用‘gets’(声明于 /usr/include/stdio.h:638) [-Wdeprecated-declarations]
  gets(password);
  ^
connect.c:159:3: 警告:不建议使用‘gets’(声明于 /usr/include/stdio.h:638) [-Wdeprecated-declarations]
  gets(server);
  ^
connect.c:217:3: 警告:不建议使用‘gets’(声明于 /usr/include/stdio.h:638) [-Wdeprecated-declarations]
  gets(name);
  ^
/tmp/ccOHKOZ3.o:在函数‘main’中:
connect.c:(.text+0x22): 警告:the `gets’ function is dangerous and should not be used.
connect.c:(.text+0xa7):对‘ECPGget_sqlca’未定义的引用
connect.c:(.text+0x2ae):对‘sqlcxt’未定义的引用
connect.c:(.text+0x336):对‘ECPGget_sqlca’未定义的引用
connect.c:(.text+0x50b):对‘sqlcxt’未定义的引用
connect.c:(.text+0x57b):对‘ECPGget_sqlca’未定义的引用
connect.c:(.text+0x5b8):对‘sqlcxt’未定义的引用

解决办法:

ECPGget_sqlca’未定义的引用
编译时增加 -lecpg
对‘sqlcxt’未定义的引用
编译时增加 -L/usr/lib/oracle/18.3/client64/lib -lclnts

正确的编译命令:

[linuxidc@ Oracle~]$ gcc -o connect connect.c -L/usr/lib/oracle/18.3/client64/lib -lclntsh -lecpg
[linuxidc@ Oracle~]$ ls
connect connect.pc  connect.c  connect.lis

编译成功,生成可执行文件。

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计3024字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中