2014-03-12 101 views
0

我試圖訪問具有特殊字符名稱中的一個表的錶行,有人可以幫助請:(如何逃避特殊字符在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; 

錯誤:

Name "main::CHAT_EVENT_QUEUE_table" used only once: possible typo at ./t21 line 30. Use of uninitialized value $CHAT_EVENT_QUEUE_table in concatenation (.) or string at ./t21 line 30. DBD::Oracle::db prepare failed: ORA-01756: quoted string not properly terminated (DBD ERROR: OCIStmtPrepare) [for Statement "SELECT q.user_data.chat_event_text, q.enq_time FROM swapp_owner.aq' 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; "] at ./t21 line 30. DBD::Oracle::db prepare failed: ORA-01756: quoted string not properly terminated (DBD ERROR: OCIStmtPrepare) [for Statement "SELECT q.user_data.chat_event_text, q.enq_time FROM swapp_owner.aq' 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; "] at ./t21 line 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); } 
+0

我已經添加完整的腳本。請看看 – fiddle

+0

嗯,[DBI](http://search.cpan.org/dist/DBI/DBI.pm)'$ sql = $ dbh-> quote($ value);'請參閱鏈接的文檔。 – alexmac

回答

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 ...)); 

如果由於某種原因你需要跳出代碼,逃避$\$

+0

可否請您寫下整個陳述。 – fiddle

+0

This worked: 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.enq_time = p.enq_time order by q.enq_time desc「); – fiddle

+0

perl從哪裏借用了這個WTF? :) –