简介:oracle数据库在windows上,虚拟机ubuntu上装Pro*C开发环境,通过此种方式进行连接。
通过proc预编译工具将嵌入的sql语句翻译成C语句,最后使用gcc进行编译链接。在预编译的时候是将嵌入的sql语句翻译成OCI的调用,原来Proc是对OCI的另外一种封装。
下面就来搭建Pro*c的开发环境吧。这里有各种Pro*c的库,只有pro*c库还是不行的,我们还需要有oracle的客户端才行,Get Oracle Instant Client->Download,转到下载页面之后按照相应的版本下载相应的安装包,不过不用全部都要下载,需要的安装包有:
wang@wang:~/app_oracle$ uname -a
Linux wang 4.4.0-148-generic #174-Ubuntu SMP Tue May 7 12:20:14 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-sqlplus-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
其中的 11.2.0.4.0 是版本号~~按需下载!,首选安装 -basic包 其余的没有顺序要求了
wang@wang:~$ sudo apt install rpm
...
wang@wang:~$ rpm -v
RPM version 4.12.0.1
Copyright (C) 1998-2002 - Red Hat, Inc.
This program may be freely redistributed under the terms of the GNU GPL
rpm -ivh rpm包文件 //安装
-i:表示安装
-v:表示可视化
-h:表示显示安装进度
wang@wang:~$ rpm -ivh ./Desktop/oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
rpm: RPM should not be used directly install RPM packages, use Alien instead!
rpm: However assuming you know what you are doing...
error: Failed dependencies:
libaio is needed by oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64
所以不是这么玩的,Ubuntu的软件包格式是deb,如果要安装rpm的包,则要先用alien把rpm转换成deb。
sudo apt-get install alien #alien默认没有安装,所以首先要安装它
sudo alien xxxx.rpm #将rpm转换位deb,完成后会生成一个同名的xxxx.deb
sudo dpkg -i xxxx.deb #安装
wang@wang:~$ sudo apt-get install alien
wang@wang:~$ sudo alien ./Desktop/oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-basic_11.2.0.4.0-2_amd64.deb generated
wang@wang:~$ sudo alien ./Desktop/oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-sqlplus_11.2.0.4.0-2_amd64.deb generated
wang@wang:~$ sudo alien ./Desktop/oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-devel_11.2.0.4.0-2_amd64.deb generated
wang@wang:~$ sudo alien ./Desktop/oracle-instantclient11.2-precomp-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-precomp_11.2.0.4.0-2_amd64.deb generated
wang@wang:~$ sudo dpkg -i oracle-instantclient11.2-basic_11.2.0.4.0-2_amd64.deb [sudo] password for wang:
(Reading database ... 699597 files and directories currently installed.)
Preparing to unpack oracle-instantclient11.2-basic_11.2.0.4.0-2_amd64.deb ...
Unpacking oracle-instantclient11.2-basic (11.2.0.4.0-2) ...
Setting up oracle-instantclient11.2-basic (11.2.0.4.0-2) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
wang@wang:~$ sudo dpkg -i oracle-instantclient11.2-sqlplus_11.2.0.4.0-2_amd64.deb
Selecting previously unselected package oracle-instantclient11.2-sqlplus.
(Reading database ... 699615 files and directories currently installed.)
Preparing to unpack oracle-instantclient11.2-sqlplus_11.2.0.4.0-2_amd64.deb ...
Unpacking oracle-instantclient11.2-sqlplus (11.2.0.4.0-2) ...
Setting up oracle-instantclient11.2-sqlplus (11.2.0.4.0-2) ...
wang@wang:~$ sudo dpkg -i oracle-instantclient11.2-devel_11.2.0.4.0-2_amd64.deb Selecting previously unselected package oracle-instantclient11.2-devel.
(Reading database ... 699623 files and directories currently installed.)
Preparing to unpack oracle-instantclient11.2-devel_11.2.0.4.0-2_amd64.deb ...
Unpacking oracle-instantclient11.2-devel (11.2.0.4.0-2) ...
Setting up oracle-instantclient11.2-devel (11.2.0.4.0-2) ...
wang@wang:~$ sudo dpkg -i oracle-instantclient11.2-precomp_11.2.0.4.0-2_amd64.deb
Selecting previously unselected package oracle-instantclient11.2-precomp.
(Reading database ... 699672 files and directories currently installed.)
Preparing to unpack oracle-instantclient11.2-precomp_11.2.0.4.0-2_amd64.deb ...
Unpacking oracle-instantclient11.2-precomp (11.2.0.4.0-2) ...
Setting up oracle-instantclient11.2-precomp (11.2.0.4.0-2) ...
安装目录:/usr/lib/oracle/11.2/client64
头文件目录:/usr/include/oracle
全部安装成功就OK了。which proc 看看有没有proc的命令吧!
wang@wang:~/app_oracle$ which proc
/usr/bin/proc
/etc/profile文件中加入相应路径,不然找不到库:
proc: error while loading shared libraries: libclntsh.so.11.1: cannot open shared object file: No such file or directory
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
下面先测试一下能不能链接我们的数据库吧!
test.pc:
用户system,密码manager(或者其他如scott/tiger),地址是虚拟机所在windows的地址,端口配置的是监听1522,实例orcl。
#include <stdio.h>
#include <sqlca.h>
#include <oraca.h>
EXEC ORACLE OPTION(ORACA=YES);
EXEC SQL BEGIN DECLARE SECTION;
char *uid="system/manager@192.168.x.x:1522/orcl";
EXEC SQL END DECLARE SECTION;
int main()
{
EXEC SQL CONNECT :uid;
if (sqlca.sqlcode == 0)
printf("Connect successful!\n");
else
printf("%s\n",sqlca.sqlerrm.sqlerrmc);
return sqlca.sqlcode;
}
编译处理:
1、将.pc文件处理成.c文件,执行成功之后会有一个test.c文件
wang@wang:~/app_oracle$ proc parse=none test.pc
Pro*C/C++: Release 11.2.0.4.0 - Production on Tue May 28 10:53:34 2019
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
System default option values taken from: /usr/lib/oracle/11.2/client64/lib/precomp/admin/pcscfg.cfg
2、根据.c文件生成可执行程序
wang@wang:~/app_oracle$ gcc -o test test.c -lclntsh -I/usr/include/oracle/11.2/client64 -L/usr/lib/oracle/11.2/client64/lib
test.c:117:8: warning: type defaults to ‘int’ in declaration of ‘sqlcxt’ [-Wimplicit-int]
extern sqlcxt (/*_ void **, unsigned int *,
^
test.c:119:8: warning: type defaults to ‘int’ in declaration of ‘sqlcx2t’ [-Wimplicit-int]
extern sqlcx2t(/*_ void **, unsigned int *,
^
test.c:121:8: warning: type defaults to ‘int’ in declaration of ‘sqlbuft’ [-Wimplicit-int]
extern sqlbuft(/*_ void **, char * _*/);
^
test.c:122:8: warning: type defaults to ‘int’ in declaration of ‘sqlgs2t’ [-Wimplicit-int]
extern sqlgs2t(/*_ void **, char * _*/);
^
test.c:123:8: warning: type defaults to ‘int’ in declaration of ‘sqlorat’ [-Wimplicit-int]
extern sqlorat(/*_ void **, unsigned int *, void * _*/);
^
3、运行
wang@wang:~/app_oracle$ ./test
Connect successful!
说明链接成功了,否则会打印链接失败的信息。
至此Pro*c开发环境搭建完成。