软件需求: 1,GBase 8s CSDK 2,perl-5.16.3 及perl-devel-5.16.3 3,unixODBC-2.3.1 4,DBD-ODBC-1.60 ###1,系统环境确认 ####1.1 perl、perl-devel及unixODBC版本 Linux下root用户通过rpm确认perl、perl-devel,unixODBC、unixODBC-devel均已经安装,如下:
[root@rhel7u6 ~]# rpm -qa perl perl-devel unixODBC unixODBC-devel
perl-5.16.3-293.el7.x86_64
perl-devel-5.16.3-293.el7.x86_64
unixODBC-devel-2.3.1-11.el7.x86_64
unixODBC-2.3.1-11.el7.x86_64
####1.2 GBase 8s CSDK已经安装 Linux下gbasedbt用户通过esql -V命令确认GBase 8s CSDK已经安装,如下:
[gbasedbt@rhel7u6 ~]$ esql -V
GBase CSDK Version 4.10, GBASE-ESQL Version 4.10.FC4G1_3.0.0
Software Serial Number AAA#B000000
###2,ODBC配置 ####2.1 ODBC配置文件 修改/etc/odbc.ini配置文件,增加GBase 8s数据库连接信息
;---------------------------------------------------------------------------
; GBase ODBC Sample File
;
; File: odbc.ini
;
;---------------------------------------------------------------------------
[ODBC Data Sources]
testdb=GBase ODBC DRIVER
;
; Define ODBC Database Driver's Below - Driver Configuration Section
;
[testdb]
Driver=/opt/gbasedbt/lib/cli/iclis09b.so
Description=GBase ODBC DRIVER
Database=testdb
LogonID=gbasedbt
pwd=GBase123
Servername=gbase01
CursorBehavior=0
CLIENT_LOCALE=zh_CN.utf8
DB_LOCALE=zh_CN.utf8
TRANSLATIONDLL=/opt/gbasedbt/lib/esql/igo4a304.so
;
; UNICODE connection Section
;
[ODBC]
;uncomment the below line for UNICODE connection
;UNICODE=UCS-4
;
; Trace file Section
;
Trace=0
TraceFile=/tmp/odbctrace.out
InstallDir=/opt/gbasedbt
TRACEDLL=idmrs09a.so
####2.2 验证ODBC配置 加载GBase 8s CSDK环境变量,包含连接到GBase 8s数据库所需的GBASEDBTDIR、LD_LIBRARY_PATH,以及选用的字符集DB_LOCALE、CLIENT_LOCALE、LANG等。加载ODBCINI环境变量。环境变量内容可保存为配置文件,如:gbase8s_odbc_env,以供调用。
export GBASEDBTDIR=/opt/gbasedbt # GBase 8s CSDK安装目录
export GBASEDBTSERVER=gbase01 # 远程GBase 8s数据库实例名称
export PATH=$GBASEDBTDIR/bin:$PATH:$HOME/bin
export DB_LOCALE=zh_CN.utf8
export CLIENT_LOCALE=zh_CN.utf8
export LD_LIBRARY_PATH=$GBASEDBTDIR/lib:$GBASEDBTDIR/lib/esql:$LD_LIBRARY_PATH
export ODBCINI=/etc/odbc.ini
通过isql命令检查ODBC配置
[root@localhost ~]# . gbase8s_odbc_env
[root@localhost ~]# isql -v testdb
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> select dbservername::varchar(40) from systables where tabid = 1;
+-----------------------------------------+
| |
+-----------------------------------------+
| gbase01 |
+-----------------------------------------+
SQLRowCount returns -1
1 rows fetched
###3,编译安装DBD-ODBC 将DBD-ODBC-1.60.tar.gz上传至GBase 8s CSDK所在的服务器上,使用root用户解压。
[root@rhel7u6 perl]# tar -zxvf DBD-ODBC-1.60.tar.gz
在生成的DBD-ODBC-1.60目录里执行perl Makefile.PL 注意:需要设置LANG=C,若设置为zh_CN.UTF-8会有个警告
[root@rhel7u6 DBD-ODBC-1.60]# export LANG=C
[root@rhel7u6 DBD-ODBC-1.60]# perl Makefile.PL
make 操作的过程中有警告,暂时忽视之
[root@rhel7u6 DBD-ODBC-1.60]# make
[root@rhel7u6 DBD-ODBC-1.60]# make install
###4,确认DBD::ODBC已经安装 通过命令查询DBD::ODBC组件已经安装,显示其版本号
[root@rhel7u6 ~]# perl -MDBD::ODBC -le 'print DBD::ODBC->VERSION'
1.60
###5,Perl通过DBD::ODBC连接GBase 8s数据库测试 编写测试脚本程序perl_dbi_odbc.perl,内容如下:
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
# 通过ODBC连接到数据库 testdb
my $dbh = DBI->connect
(
"DBI:ODBC:testdb",
'gbasedbt',
'GBase123'
);
# 开始事务
$dbh->do("begin");
# 删除表及创建表
my $sth_d_tab = $dbh->prepare("drop table if exists perl_dbi_gbasedbt");
$sth_d_tab->execute();
my $sth_c_tab = $dbh->prepare("create table perl_dbi_gbasedbt (id int, name varchar(40))");
$sth_c_tab->execute();
# 插入数据
my $sth_i_tab = $dbh->prepare("insert into perl_dbi_gbasedbt values(1,'测试中文输入ODBC')");
$sth_i_tab->execute();
# 查询数据
my $sth_s_tab = $dbh->prepare("select first 10 id,name from perl_dbi_gbasedbt");
$sth_s_tab->execute();
while (my $row = $sth_s_tab->fetchrow_hashref())
{
print sprintf("%d %-40s\n", $row->{id}, $row->{name});
}
# 提交事务,关闭数据库连接
$dbh->do("commit");
$dbh->disconnect();
exit 0;
perl perl_dbi_odbc.pl执行并输出结果:
[root@rhel7u6 DBD-ODBC-1.60]# perl perl_dbi_odbc.pl
1 测试中文输入ODBC
perl通过DBD::ODBC连接到GBase 8s数据库,测试成功。感谢您的阅读,gbase 数据
标签:ODBC,DBD,gbasedbt,perl,8s,Perl,GBase From: https://blog.51cto.com/u_16319042/12057780