DBD::Firebird 编码/解码

sid*_*com 2 perl firebird dbi character-encoding

在此示例中,Firebird 返回未解码的字符串。是我没有正确设置数据库还是 Firebird 就是这样工作的?

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

输出:

\n
SV = PV(0x2105360) at 0x22628a0\n  REFCNT = 1\n  FLAGS = (POK,pPOK)\n  PV = 0x22a37e0 "\\303\\244"\\0\n  CUR = 2\n  LEN = 10\n\xc3\x83\xc2\xa4\n\nSV = PV(0x2111470) at 0x2121220\n  REFCNT = 1\n  FLAGS = (POK,pPOK,UTF8)\n  PV = 0x1f2fed0 "\\303\\244"\\0 [UTF8 "\\x{e4}"]\n  CUR = 2\n  LEN = 10\n\xc3\xa4\n
Run Code Online (Sandbox Code Playgroud)\n

Mar*_*eel 5

正如注释中由Clip提供的 DBD::Firebird 文档的链接所示,您需要使用以下命令进行连接:

$dbh = DBI->connect(
    "dbi:Firebird:db=$db;ib_charset=UTF8", 'user', 'password',
    { PrintError => 0, RaiseError => 1, ib_enable_utf8 => 1 }
Run Code Online (Sandbox Code Playgroud)

也就是说,该ib_charset属性必须位于连接字符串中,而不是散列中,并且其值必须为UTF8(不是UTF-8!),并且需要将ib_enable_utf8with 设置添加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 } );
Run Code Online (Sandbox Code Playgroud)