DBD::火鸟编码/解码



在本例中,Firebird返回未解码的字符串。我是否没有正确设置数据库,或者这是火鸟的工作方式?

#!/usr/bin/env perl
use warnings;
use 5.10.0;
use utf8;
use open qw( :std :utf8 );
use Devel::Peek;
use DBI;
my ( $db, $dbh, $sth, @array );
my $table = 'test_encoding';
my $create = "CREATE TABLE $table ( name varchar(32) )";
my $insert = "INSERT INTO $table ( name ) VALUES ( ? )";
my $select = "SELECT * FROM $table";
my $value = 'ä';
$db = '/home/me/my_firebird_db';
$dbh = DBI->connect(
"dbi:Firebird:db=$db", 'user', 'password',
{ PrintError => 0, RaiseError => 1, ib_charset => 'UTF-8' }
);
$sth = $dbh->do( "DROP TABLE $table" );
$sth = $dbh->do( $create );;
$sth = $dbh->prepare( $insert );
$sth->execute( $value );
@array = $dbh->selectrow_array( $select );
Dump $array[0];
say $array[0];
say "";
$db = '/home/me/my_sqlite_db';
$dbh = DBI->connect(
"dbi:SQLite:db=$db", '', '',
{ PrintError => 0, RaiseError => 1, sqlite_string_mode => 5 }
);
$sth = $dbh->do( "DROP TABLE IF EXISTS $table" );
$sth = $dbh->do( $create );
$sth = $dbh->prepare( $insert );
$sth->execute( $value );
@array = $dbh->selectrow_array( $select );
Dump $array[0];
say $array[0];

输出:

SV = PV(0x2105360) at 0x22628a0
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x22a37e0 "303244"
CUR = 2
LEN = 10
ä
SV = PV(0x2111470) at 0x2121220
REFCNT = 1
FLAGS = (POK,pPOK,UTF8)
PV = 0x1f2fed0 "303244" [UTF8 "x{e4}"]
CUR = 2
LEN = 10
ä

正如注释中clamp提供的到DBD::Firebird文档的链接所示,您需要使用:

进行连接
$dbh = DBI->connect(
"dbi:Firebird:db=$db;ib_charset=UTF8", 'user', 'password',
{ PrintError => 0, RaiseError => 1, ib_enable_utf8 => 1 }

ib_charset属性必须在连接字符串中,不能在散列中,且其值必须为UTF8(而不是UTF-8!),需要将设置为1ib_enable_utf8添加到散列中。

具体来说,这部分:

ib_enable_utf8 (driver-specific, boolean)

将此属性设置为TRUE将产生任何Perl Unicode字符串作为语句参数提供,将被降级为八位字节序列然后再传给火鸟。

另外,从数据库中检索到的任何字符数据(CHAR, VARCHAR,BLOB sub_type TEXT)将升级为Perl Unicode字符串。

注意:目前只支持ib_charsetDSN参数为UTF8。在未来,编码和解码到/从可以实现任意字符集

的例子:

$dbh = DBI->connect( 'dbi:Firebird:db=database.fdb;ib_charset=UTF8',
{ ib_enable_utf8 => 1 } );

最新更新