如何在perl中的表行中转义特殊字符

时间:2014-03-12 11:39:01

标签: mysql sql oracle perl

我正在尝试访问名称中有特殊字符的表格,有人可以帮忙:(

SELECT 
  q.user_data.chat_event_text, q.enq_time
FROM 
  swapp_owner.aq'$'CHAT_EVENT_QUEUE_table q,
  CHAT_EVENT_QUEUE_table p
where 
      q.expiration_reason = 'TIME_EXPIRATION'
  and q.msg_id=p.msg_Id
  and p.enq_time > (SYSDATE - 50000/(24*60))
order by 
  q.enq_time desc;

错误:

  

姓名" main :: CHAT_EVENT_QUEUE_table"仅使用一次:可能的拼写错误   ./t21第30行。使用未初始化的值$ CHAT_EVENT_QUEUE_table in   连接(。)或字符串在./t21第30行.DBD :: Oracle :: db prepare   失败:ORA-01756:引用的字符串未正确终止(DBD错误:   OCIStmtPrepare)[for Statement" SELECT q.user_data.chat_event_text,   q.enq_time来自swapp_owner.aq' q,CHAT_EVENT_QUEUE_table p其中   q.expiration_reason =' TIME_EXPIRATION'和q.msg_id = p.msg_Id和   p.enq_time> (SYSDATE - 50000 /(24 * 60))按q.enq_time desc排序; "] at   ./t21第30行.DBD :: Oracle :: db准备失败:ORA-01756:引用   字符串没有正确终止(DBD ERROR:OCIStmtPrepare)[for   声明" SELECT q.user_data.chat_event_text,q.enq_time FROM   swapp_owner.aq' q,CHAT_EVENT_QUEUE_table p其中q.expiration_reason =   ' TIME_EXPIRATION'和q.msg_id = p.msg_Id和p.enq_time> (SYSDATE -   50000 /(24 * 60))q.enq_time desc的订单; "]在./t21第30行。

添加完整的脚本:

!/usr/bin/perl -w


BEGIN {   $ENV{ORACLE_HOME}='/u01/app/oracle/product/11.1.0/'; } use strict;

use DBI; use utf8;

my $DB='pre14msv'; my $db_user='SWAPP_OWNER'; my $password=`/usr/bin/pmo view password -u $db_user -t $DB`; chomp($password); my $db = DBI->connect( "dbi:Oracle:pre14msv", $db_user, $password )

    || die( $DBI::errstr . "\n" );

$db->{AutoCommit}    = 0;

$db->{RaiseError}    = 1;

$db->{ora_check_sql} = 0;

$db->{RowCacheSize}  = 16;


my $sth = $db->prepare("SELECT q.user_data.chat_event_text, p.enq_time FROM swapp_owner.aq\$\CHAT_EVENT_QUEUE_table q,CHAT_EVENT_QUEUE_table p where q.expiration_reason = 'TIME_EXPIRATION' and q.msg_id=p.msg_Id and p.enq_time > (SYSDATE - 50000/(24*60)) order by q.enq_time desc; "); $sth->execute();

while ( my @row = $sth->fetchrow_array() ) {
    foreach (@row) {
        $_ = "\t" if !defined($_);
        print "$_\t";
    }
    print "\n"; }


print "If you see this, execute phase succeeded without a problem.\n";

END {

    $db->disconnect if defined($db); }

1 个答案:

答案 0 :(得分:2)

在Perl中,'内部变量名称与::相同,因此$'CHAT_EVENT_QUEUE_table变为$::CHAT_EVENT_QUEUE_table,它是包main中的包变量:$main::CHAT_EVENT_QUEUE_table - 除了你没有这样的变量。

如果您希望文字字符串$'CHAT_EVENT_QUEUE_table存在,请将SQL放入单引号字符串中,该字符串不插入变量,例如

my $sth = $db->prepare(q(... SQL here no $'variables ...));

如果由于某种原因您确实需要转义码,请转义$\$