李士朝
发布于

suse 11 sp3 perl连接OushuDB,从编译到测试

1. 编译

1.1 所需要的安装包

DBD-ODBC-1.61.tar.gz
DBI-1.643.tar.gz
unixODBC-2.2.12.tar.gz
psqlodbc-08.02.0500.tar.gz
pgsql-V1.1.tar.gz

1.2 解压安装包

tar -xzvf DBD-ODBC-1.61.tar.gz
tar -xzvf DBI-1.643.tar.gz
tar -xzvf unixODBC-2.2.12.tar.gz
tar -xzvf psqlodbc-08.02.0500.tar.gz
tar -xzcf pgsql-V1.1.tar.gz

1.3 编译

cd unixODBC-2.2.12
./configure --enable-gui=no
make
make install

cd DBI-1.643
perl Makefile.PL
make
make install

cd DBD-ODBC-1.61
ODBCHOME=/usr/local/unixODBC/
LD_LIBRARY_PATH=/usr/local/unixODBC/lib:$LD_LIBRARY_PATH
export ODBCHOME
export LD_LIBRARY_PATH
perl Makefile.PL -o /etc/unixODBC
make
make install

cd psqlodbc-08.04.0500
export pg_config = ~/pgsql/bin/pg_config
./configure --prefix= /data/psqlodbc --disable-openssl --without-libpq
make
make install

2. 配置

vim /etc/unixODBC/odbc.ini 
[OushuDB]
Driver = OushuDB
Description = HAWQ DSN
Servername = 10.0.192.239
Database = postgres
Username = oushu
Password = oushu666
Port = 5432
ReadOnly = No
Fetch = 10000
UseDeclareFetch = 1

vim /etc/unixODBC/odbcinst.ini 
[OushuDB]
Description = OushuDB driver for linux
#Driver = /usr/lib64/unixODBC/libodbcpsql.so.1
Driver = /data/psqlodbc/lib/psqlodbcw.so
Setup = /usr/lib64/unixODBC/libodbcpsqlS.so.1

3. 测试

3.1 isql

isql -v OushuDB

3.2 perl

#!/usr/bin/perl

#perl内部模块
use warnings;
use strict;
use DBI;

#程序开始执行
my $vi_return = main();
exit($vi_return);
#程序执行结束

sub main 
{
  #执行语句
  my $sql = "";
  
  #返回影响行数
  my $rows = 0;
  
  #执行结果
  my $vi_err_code = "0";
  my $vv_err_msg = "OK";
  my $vh_result_info;
  $vh_result_info->{result_code} = $vi_err_code;
  $vh_result_info->{result_msg} = $vv_err_msg;
  $vh_result_info->{rows} = $rows;
  
  #connect oushudb
  my $dbh;
  my $dbname = "OushuDB";
  my $dbuser = "oushu";
  my $password = "oushu666";
  #my $password = "123456";
  
  my $sysloginfo;
  $sysloginfo->{task_id} = 0;
  $sysloginfo->{task_name} = "jx";
  $sysloginfo->{table_name} = "jx";
  $sysloginfo->{task_sign} = "jx";
  $sysloginfo->{task_status} = 1;
  $sysloginfo->{task_log} = "init";
  $sysloginfo->{task_pos} = "程序开始日志";
  $sysloginfo->{rowcount} = 0;
  $sysloginfo->{schema_name} = "jx";
  $sysloginfo->{run_user_name} = "jx";
  

  #$dbh = connectDb($dbname,$dbuser,$password);
  $dbh = DBI->connect("dbi:ODBC:$dbname", $dbuser, $password , {
           PrintError => 1,   ### Don't report errors via warn
              RaiseError => 1,   ### Don't report errors via die
              AutoCommit => 0
          });

  if (!$dbh)
  {
        print "db connect failed!\n";
        exit(12);
  }
  else
  {
        print "db connect success!\n";
        #disconnectDb();
        #exit(0);
  }
    
#  $sql = "delete from  masamk.tb_mk_se_balance_wj_ims_mid1";
  $sql = 'select count(1) from id limit 1';
  my $sth = $dbh->prepare($sql) or warn "get_column: unable to process:$sql\n";
  my $rv = $sth->execute;
  my @result = $sth->fetchrow_array();
  my $re_con = $result[0];
  #my @result=$dbh->selectrow_array($sql);
  #dump(@result);

  print "jx:$re_con\n";
  $sth->finish; 

  #my @chk =$dbh->selectrow_array("select count(1) as c1 from masamk.tb_mk_sc_user_mon t where t.statis_month = 202204 ");
  #print "chk is @chk \n";

#  print "rv is $rv";
#  print "sth is $sth";
#  print "result is @result \n";
#  print "re_con is $re_con";
 
$dbh-> disconnect()  if $dbh ;

#    $sql = qq { truncate table masamk.tb_mk_se_balance_wj_ims_mid1;
#              truncate table masamk.tb_mk_se_balance_wj_ims_mid2;
#              truncate table masamk.tb_mk_se_balance_wj_tmp3;
#              truncate table masamk.tb_mk_se_balance_wj_tmp4;
#    };
#    $rows = execSqlW($sql,$sysloginfo);
#     $rows = $dbh->do($sql) or warn "execSql:Unable to process: $sql\n";
#        my ($err,$state,$errmsg) = ($dbh->err,$dbh->state,$dbh->errstr);
#        if (!defined($errmsg) or substr($errmsg,0,12) eq "WARNING 3028")
#        {
#          $state =0;
          #commitDb();  #2016-5-30 22:42 成功则提交事务 
#         $dbh-> commit  if $dbh ;
#         print "execute success\n";
#        }
#        else
#        {
#          $state =1;
#        }

#     print "$rows";
}

4. 可能出现的问题

DBD::ODBC::st fetchrow_array warning: no select statement currently executing at 1.pl line 57.
Use of uninitialized value $re_con in string at 1.pl line 60.
Use of uninitialized value $re_con in concatenation (.) or string at 1.pl line 64.
评论
    test