我试图了解这里发生了什么。
我正在使用 Perl 和 DBI::Oracle 连接到我的 Oracle XE 数据库。我想在我的数据库查询中获取 UTF 编码字符作为返回值。不幸的是,我的期望没有得到满足。如果您仔细查看返回值,就会清楚这一点(从第 4 行开始)。对于变音符号“ä”,我得到的不是 c3a4,而是 e4。
有人可以向我解释为什么会发生这种情况吗
user@pc:~$ cat charset_test.pl
use DBI;
use Data::Dumper;
binmode STDOUT, ":utf8";
my $dbname = "DB:1521/XE";
my $user = "system";
my $passwd = "password";
$dbh = DBI->connect("dbi:Oracle:$dbname", $user, $passwd);
sub sql{
my $sql = shift;
$dbh->{RaiseError} = 1;
$sth = $dbh->prepare($sql);
$sth->execute;
my @dbrow = $sth->fetchrow_array;
return $dbrow[0];
}
printf( " 0: NLS_LANG %s \n", $ENV{'NLS_LANG'});
printf( " 1: pack ae %s \n", unpack( 'H*', "ä" ) );
printf( " 2: sql rawtohex ae %s \n", sql("select rawtohex(ae) from (select UTL_I18N.RAW_TO_CHAR ('c3a4', 'AL32UTF8') as ae from dual)"));
printf( " 3: sql rawtohex('ae') %s \n", sql("select rawtohex('ä') from dual") ) ;
printf( " 4: sql 'ae' %s", Dumper( sql("select 'ä' from dual") ) );
printf( " 5: sql 'ae' %s \n", sql("select 'ä' from dual") ) ;
printf( " 6: pack sql ae %s \n", unpack( 'H*', sql("select ae from (select UTL_I18N.RAW_TO_CHAR ('c3a4', 'AL32UTF8') as ae from dual)")));
printf( " 7: pack sql 'ae' %s \n", unpack ('H*', sql("select 'ä' from dual") ) );
exit;
user@pc:~$ NLS_LANG=AMERICAN_AMERICA.UTF8 perl charset_test.pl
0: NLS_LANG AMERICAN_AMERICA.UTF8
1: pack ae c3a4
2: sql rawtohex ae C3A4
3: sql rawtohex('ae') C3A4
4: sql 'ae' $VAR1 = "\x{e4}";
5: sql 'ae' ä
6: pack sql ae e4
7: pack sql 'ae' e4