在此示例中,Firebird 回傳未解碼的字串。我沒有正確設定資料庫還是 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 "\303\244"\0
CUR = 2
LEN = 10
?¤
SV = PV(0x2111470) at 0x2121220
REFCNT = 1
FLAGS = (POK,pPOK,UTF8)
PV = 0x1f2fed0 "\303\244"\0 [UTF8 "\x{e4}"]
CUR = 2
LEN = 10
?
uj5u.com熱心網友回復:
如評論中由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!),并且您需要將ib_enable_utf8with setting添加1到哈希中。
具體來說,這部分:
ib_enable_utf8(特定于驅動程式,布林值)
將此屬性設定為 TRUE 將導致作為陳述句引數提供的任何 Perl Unicode 字串在傳遞給 Firebird 之前降級為八位位元組序列。
此外,從資料庫中檢索到的任何字符資料(CHAR、VARCHAR、BLOB sub_type TEXT)都將升級為 Perl Unicode 字串。
警告:目前僅當ib_charset DSN 引數為時才支持此功能
UTF8。將來,可以實作對任意字符集的編碼和解碼。例子:
$dbh = DBI->connect( 'dbi:Firebird:db=database.fdb;ib_charset=UTF8', { ib_enable_utf8 => 1 } );
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/494023.html
下一篇:如何在Perl中僅列印呼叫者函式
