guoke0073 2014-04-22 07:50 采纳率: 0%
浏览 1019

perl 如何存储超过4000的字段进入oracle

if ($row->{interfacetype} eq 'DATA') {
my $sth = $dbh->prepare(qq{
INSERT INTO DEV_AUDIT_RECORD (
RECORD_ID, CREATE_TIME, MEMBER_ID, AUDIT_STATE, AUDIT_TYPE, AUDIT_CONTENT, AUDIT_CONTENT_ORIGIN,
PUBLISHER_ID, PUBLISHER_NICKNAME, PUBLISHER_IP, PUBLISH_TIME,
SECTION, MESSAGE_ID, MESSAGE_ORIGIN, MESSAGE_CLASS, MESSAGE_REPRINT,
CONTENT_ID, CONTEXT_ID, CONTEXT,
ORDER_TYPE, USER_TYPE)
VALUES (
DEV_AUDIT_RECORD_ID_SEQUENCE.nextval, :create_time, :member_id, :audit_state, :audit_type,?, :audit_content_origin,
:publisher_id, :publisher_nickname, :publisher_ip, :publish_time,
:section, :message_id, :message_origin, :message_class, :message_reprint,
:content_id, :context_id, :context,
:order_type, :user_type)
RETURNING RECORD_ID INTO :record_id
});

if (!$sth or $dbh->err()) {
$logger->info($dbh->errstr());
}
else{
use utf8;
my ($commp, $dept, $product, $sub) = get_section($row->{section});
my $message_origin = encode('gbk', decode('utf-8', $product));
my $message_class = encode('gbk', decode('utf-8', $sub));
my $content_nohtml = $msg->{CONTENT};
$content_nohtml =~ s/<\/?[^>]*>//ig;
$sth->bind_param(":create_time", strftime('%Y-%m-%d %H:%M:%S', localtime()));
$sth->bind_param(":member_id", $msg->{USERID});
$sth->bind_param(":audit_state", 0);
$sth->bind_param(":audit_type", 1); #$row->{audit_type}
$sth->bind_param(":audit_content_origin", $content_nohtml);
$sth->bind_param(":publisher_id", $msg->{USERID});
$sth->bind_param(":publisher_nickname", $msg->{NICKNAME});
$sth->bind_param(":publisher_ip", $msg->{USERIP});
$sth->bind_param(":publish_time", $msg->{PUBTIME});
$sth->bind_param(":section", $row->{section});
$sth->bind_param(":message_id", $row->{messageid});
$sth->bind_param(":message_origin", $message_origin);
$sth->bind_param(":message_class", $message_class);
$sth->bind_param(":message_reprint", $row->{contenttype});
$sth->bind_param(":content_id", $msg->{CONTENTID});
$sth->bind_param(1, $msg->{CONTENT});
$sth->bind_param(":context_id", $msg->{CONTEXTID});
$sth->bind_param(":context", $msg->{CONTEXT});
$sth->bind_param(":order_type", $row->{priority});
$sth->bind_param(":user_type", $msg->{USERTYPE});
$sth->bind_param_inout(":record_id", \my $new_id, 1e17);
$sth->execute;
if ($dbh->err()) {

写入保存失败的新增数据

$logger->debug("store to the file:".$config{restoreDirectory}."/saved.dat");
#my $db = DBM::Deep->new(

file => $config{restoreDirectory}."/saved.dat",

type => DBM::Deep->TYPE_ARRAY

);

#my $len = $db->length();
#$db->put($len, $row);
$logger->error($dbh->errstr(enter code here

其中AUDIT_CONTENT为超过4000字符的字段,oracle使用的是clob。 运行报错为ORA-01461: can bind a LONG value only for insert into a LONG column (DBD ERROR: OCIStmtExecute)
查询过java在处理这个种情况的时候是调用的oracle的组件,php在处理这个情况的时候使用 php_oci扩展,问下perl在遇到这种情况下有什么解决方案,在不拆字段,不组装数据的前提下

  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-03-09 12:25
    关注

    该回答引用ChatGPT

    在 Perl 中,您可以使用 DBD::Oracle 模块来连接和操作 Oracle 数据库。针对超过 4000 个字符的字段,可以使用 DBD::Oracle 提供的 ora_lob_prefetch_size 和 ora_lob_read_size 属性来处理。ora_lob_prefetch_size 属性指定了预读取 LOB 数据时的块大小,ora_lob_read_size 属性指定了从数据库中读取 LOB 数据时的块大小。通过调整这两个属性的值,可以使 DBD::Oracle 在插入 LOB 数据时避免超出缓冲区的限制。


    下面是一个示例代码:

    my $dbh = DBI->connect("dbi:Oracle:$dbname", $user, $password, {RaiseError => 1});
    $dbh->{ora_lob_prefetch_size} = 1024*1024; # 1MB
    $dbh->{ora_lob_read_size} = 1024*1024; # 1MB
    
    my $sth = $dbh->prepare(qq{
      INSERT INTO MY_TABLE (ID, LOB_FIELD) VALUES (?, ?)
    });
    
    my $id = 1;
    my $lob_data = "..." x 10000; # 40000 bytes of data
    
    $sth->bind_param(1, $id);
    $sth->bind_param(2, $lob_data, {ora_type => ORA_CLOB});
    
    $sth->execute();
    

    在上面的代码中,我们将 ora_lob_prefetch_size 和 ora_lob_read_size 属性设置为 1MB,这样可以确保 DBD::Oracle 能够处理最大为 1MB 的 LOB 数据。我们使用 bind_param() 方法将 LOB 数据绑定到 SQL 语句中,其中 {ora_type => ORA_CLOB} 指定了数据类型为 CLOB。最后,我们执行 SQL 语句来将 LOB 数据插入到数据库中。


    注意,如果您使用的是旧版本的 DBD::Oracle 模块,那么您可能需要手动设置 ora_clob_refetch 属性来避免出现 ORA-01461 错误:

    $dbh->{ora_clob_refetch} = 1;
    

    在最新版本的 DBD::Oracle 模块中,这个问题已经得到了解决,您不需要手动设置此属性。

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler