GBase 8s数据库连接 – Perl(DBD::GBasedbt)

操作系统:RHEL 7U6 64bit
软件需求:
1,GBase 8s CSDK
2,perl-5.16.3
3,DBD-GBasedbt-2018.1031(Base on: DBD-Informix-2018.1031)

1,系统环境确认

1.1 perl版本

Linux下root用户通过rpm确认perl、perl-DBI、perl-Test-Pod、perl-Test-Pod-Coverage及perl-ExtUtils-MakeMaker均已经安装,如下:

[root@rhel7u6 perl]# rpm -qa perl perl-DBI perl-Test-Pod perl-Test-Pod-Coverage  perl-ExtUtils-MakeMaker
perl-5.16.3-293.el7.x86_64
perl-Test-Pod-1.48-3.el7.noarch
perl-ExtUtils-MakeMaker-6.68-3.el7.noarch
perl-DBI-1.627-4.el7.x86_64
perl-Test-Pod-Coverage-1.08-21.el7.noarch

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,DBD-GBasedbt编译安装

2.1 环境变量加载

加载GBase 8s CSDK环境变量,包含连接到GBase 8s数据库所需的GBASEDBTDIR、GBASEDBTSERVER、PATH、LD_LIBRARY_PATH,以及选用的字符集DB_LOCALE、CLIENT_LOCALE、LANG等。环境变量内容可保存为配置文件,如:gbase8s_csdk_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

# 以下4个环境变量仅用于编译时的测试,testdb应当存在
export DBI_DBNAME=testdb
export DBD_GBASEDBT_DATABASE=testdb
export DBD_GBASEDBT_USERNAME=gbasedbt
export DBD_GBASEDBT_PASSWORD=GBase123

2.2 编译安装DBD-GBasedbt

将DBD-GBasedbt-2018.1031.tar.gz上传至GBase 8s CSDK所在的服务器上,使用root用户解压,加载环境变量文件gbase8s_csdk_env。

[root@rhel7u6 perl]#  .  gbase8s_csdk_env
[root@rhel7u6 perl]#  tar -zxvf DBD-GBasedbt-2018.1031.tar.gz

在生成的DBD-GBasedbt-2018.1031目录里执行perl Makefile.PL

[root@rhel7u6 DBD-GBasedbt-2018.1031]# perl Makefile.PL
[root@rhel7u6 DBD-GBasedbt-2018.1031]# make 
[root@rhel7u6 DBD-GBasedbt-2018.1031]# make install

3,确认DBD::GBasedbt已经安装

通过命令查询DBD::GBasedbt组件已经安装,显示其版本号

[root@rhel7u6 ~]#  perl -MDBD::GBasedbt -le 'print DBD::GBasedbt->VERSION'
2018.1031

4,Perl通过DBD::GBasedbt连接GBase 8s数据库测试

编写测试脚本程序perl_dbi_gbasedbt.pl,内容如下:

#!/usr/bin/perl

use warnings;
use strict;
use DBI;

# 连接到数据库 testdb
my $dbh = DBI->connect
          (
             "DBI:GBasedbt:testdb",
             'gbasedbt',
             'GBase123',
             { PrintError => 0, RaiseError => 1, AutoCommit => 0 }
          );

# 开始事务
$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,'测试中文输入')");
$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_gbasedbt.pl执行并输出结果:

[root@rhel7u6 DBD-GBasedbt-2018.1031]# perl perl_dbi_gbasedbt.pl
1 测试中文输入

perl通过DBD::GBasedbt连接到GBase 8s数据库,测试成功。

标签: perl, DBD-GBasedbt, DBI::GBasedbt

添加新评论