【问题标题】:How can I insert a blob and timestamp into a table in Oracle using Perl?如何使用 Perl 将 blob 和时间戳插入 Oracle 中的表中?
【发布时间】:2021-08-21 20:53:36
【问题描述】:

我有一个如下所示的 Oracle 表。 我需要使用 perl 在此表中插入一条记录。 要插入的数据包括varchar、number、blob和timestamp。 我不插入 attr1 因为它默认为零。 另外,有没有办法获得插入的行数?

我想出了一些代码,但它不完整。我正在寻求有关如何对其进行编码的帮助,并会感谢任何帮助,尤其是因为我是 perl 的新手。

table
    attr1 INT DEFAULT 0 NOT NULL,
    attr2 VARCHAR(255) NOT NULL,
    attr3 NUMBER NOT NULL,
    attr4 BLOB NOT NULL,
    attr5 TIMESTAMP NOT NULL,
    CONSTRAINT table_pk PRIMARY KEY (attr1, attr2)
use DBD::Oracle qw(:ora_types);

sub blob_and_other_data {
    my $attr2 = shift; # varchar data looks like '38573985-45643756283'
    my $attr3 = shift; # number data looks like '-9394857384' 
    my $attr4 = shift; # blob data
    my $attr5 = shift; # timestamp data looks like '03-Jun-21 4:38:34 pm'
    my $sql_statement = "insert into table (attr2, attr3, attr4, attr5) VALUES (?, ?, ?, ?)";

    my $sth = $dbh->prepare($sql_statement);

    $sth->bind_param(1, $attr2);
    $sth->bind_param(2, $attr3, { ora_type => ? }); # not even sure if this is needed
    $sth->bind_param(3, $attr4, { ora_type => ORA_BLOB });
    $sth->bind_param(4, $attr5, { ora_type => ? }); # not even sure if this is needed

    $sth->execute();
}

【问题讨论】:

  • 我不熟悉Oracle,但似乎TIMESTAMP 数据类型描述为here,BLOB 数据类型描述为here。所以你问如何从 Perl 自定义格式转换为这些类型?

标签: sql oracle perl dbi


【解决方案1】:

时间戳我用这个,符合OTHER DATA TYPES:

my $sql_statement = "insert into table (attr5) VALUES (TO_TIMESTAMP(? ,'DD-Mon-RR HH:MI:SS AM', 'NLS_DATE_LANGUAGE=AMERICAN'))";
my $sth = $dbh->prepare($sql_statement);
$sth->bind_param(1, $attr5);
$sth->execute();

对于 LOB,我找到了 Binding for Updates and Inserts for CLOBs and BLOBs,但我从未使用过。

my $in_clob = "<document>\n";
$in_clob .= "  <value>$_</value>\n" for 1 .. 10_000;
$in_clob .= "</document>\n";
my $in_blob ="0101" for 1 .. 10_000;
 
$SQL='insert into test_lob3@tpgtest (id,clob1,clob2, blob1,blob2) values(?,?,?,?,?)';
$sth=$dbh->prepare($SQL );
$sth->bind_param(1,3);
$sth->bind_param(2,$in_clob,{ora_type=>SQLT_CHR});
$sth->bind_param(3,$in_clob,{ora_type=>SQLT_CHR});
$sth->bind_param(4,$in_blob,{ora_type=>SQLT_BIN});
$sth->bind_param(5,$in_blob,{ora_type=>SQLT_BIN});
$sth->execute();

【讨论】:

  • 非常感谢。让我试试 Wernfried。
【解决方案2】:

以下内容对我有用:

eval{blob_and_other_data($attr2, $attr3, $attr4, $attr5)};

sub blob_and_other_data {
    my $attr2 = shift;
    my $attr3 = shift;
    my $attr4 = shift;
    my $attr5 = shift;
    my $sql_statement = "insert into table (attr2, attr3, attr4, attr5) VALUES (?, ?, ?, ?)";
    
    my $sth = $dbh->prepare($sql_statement);

    $sth->bind_param(1, $attr2);
    $sth->bind_param(2, $attr3);
    $sth->bind_param(3, $attr4, { ora_type => ORA_BLOB } );
    $sth->bind_param(4, $attr5);

    $sth->execute();
}

【讨论】:

    猜你喜欢
    • 2011-07-22
    • 1970-01-01
    • 2016-08-21
    • 2014-05-10
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多