【问题标题】:Perl/DBI - Insert rows from Postgres to an Oracle tablePerl/DBI - 将行从 Postgres 插入 Oracle 表
【发布时间】:2014-08-25 00:10:13
【问题描述】:

我正在尝试编写一个脚本,该脚本将从 postgresql 表中读取数据并将其插入到 oracle 表中,这是我的脚本:

#!/usr/local/bin/perl

use strict;
use DBI;
use warnings FATAL => qw(all);

my $pgh = pgh(); # connect to postgres
my $ora = ora(); # connect to oracle
my @rows;
my $rows =[] ;
my $placeholders = join ", ", ("?") x @rows;

my $sth = $pgh->prepare('SELECT * FROM "Employees"');
$sth->execute();
 while (@rows = $sth->fetchrow_array()) {
    $ora->do("INSERT INTO employees VALUES($placeholders)");
 }

#connect to postgres
sub pgh {
my $dsn = 'DBI:Pg:dbname=northwind;host=localhost';
my $user = 'postgres';
my $pwd  = 'postgres';
my $pgh = DBI -> connect($dsn,$user,$pwd,{'RaiseError' => 1});
return $pgh;
}

#connect to oracle
sub ora {
my $dsn = 'dbi:Oracle:host=localhost;sid=orcl';
my $user = 'nwind';
my $pwd  = 'nwind';
my $ora = DBI -> connect($dsn,$user,$pwd,{'RaiseError' => 1});
return $ora;
}

我收到以下错误:

DBD::Oracle::db do failed: ORA-00936: missing expression (DBD ERROR: error possibly near <*> indicator at char 29 in 'INSERT INTO employees VALUES(<*>)') [for Statement "INSERT INTO employees VALUES()"] at /usr/share/perlproj/cgi-bin/scripts/nwind_pg2ora.pl line 19.

请帮助我纠正我的代码。 非常感谢 !! 托尼亚。

【问题讨论】:

  • 创建$placeholders 时,您的@rows 为空,或许最好先读取一行,然后在设置$ora-&gt;do(...) 之前重新创建$placeholders

标签: perl dbi


【解决方案1】:

请参阅 DBD::Oracle 的文档,您必须为 BLOB 绑定参数值,例如:

use DBD::Oracle qw(:ora_types); 
$sth->bind_param($idx, $value, { ora_type=>ORA_BLOB, ora_field=>'PHOTO' });

【讨论】:

    【解决方案2】:
    my @rows;
    my $rows =[] ;
    
    my $sth = $pgh->prepare('SELECT * FROM "Employees"');
    $sth->execute();
    while (@rows = $sth->fetchrow_array()) {
        my $placeholders = join ", ", ("?") x @rows;
        $ora->do("INSERT INTO employees VALUES($placeholders)");
    }
    

    您将加入一个空的@rows 以创建一个空的$placeholders。 在 do() 之前,在 while 循环内执行连接。

    【讨论】:

      【解决方案3】:

      以下根据返回记录中的列数懒惰地创建一个语句句柄,用于插入到 Oracle 数据库中。

      然后它将这些列值插入到数据库中,因此显然我们假设表结构是相同的:

      use strict;
      use DBI;
      use warnings FATAL => qw(all);
      
      my $pgh = pgh(); # connect to postgres
      my $ora = ora(); # connect to oracle
      
      my $sth = $pgh->prepare('SELECT * FROM "Employees"');
      $sth->execute();
      
      my $sth_insert;
      
      while (my @cols = $sth->fetchrow_array()) {
          $sth_insert ||= do {
              my $placeholders = join ", ", ("?") x @cols;
              $ora->prepare("INSERT INTO employees VALUES ($placeholders)");
          };
          $sth_insert->execute(@cols);
      }
      

      【讨论】:

      • 非常感谢!!有一个问题,postgres 中的员工表有一个数据类型为 BYTEA 的列,并插入到 BLOB 的 oracle 数据类型中,并且对此感到窒息,有没有办法我可以使用 DBI 将 BYTEA 的原始数据插入到 Oracle 中的 BLOB - Tonya
      • 我不知道,因为我自己没有使用过 Oracle。我建议这是一个新问题,您应该专门询问它。
      • 您是否查看过 DBD::Oracle 模块文档中关于使用 Oracle LOB 的主题? search.cpan.org/~pythian/DBD-Oracle-1.74/lib/DBD/…
      猜你喜欢
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2021-10-09
      • 2013-07-25
      • 2013-04-24
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多