DBD :: Firebird编码/解码

发布于 2025-02-07 11:31:29 字数 1521 浏览 2 评论 0 原文

在此示例中,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
ä

In this example, Firebird returns the string undecoded. Have I not set up the database correctly or is this how Firebird works?

#!/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];

Output:

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
ä

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话里做英雄 2025-02-14 11:31:29

dbd :: firebird clamp 在注释中,您需要使用

$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_utf8 > 1 到哈希。

具体而言,此部分

IB_ENABLE_UTF8(特定于驱动程序,布尔值)

将此属性设置为true将导致任何Perl Unicode字符串
以降级为八位字序列的语句参数提供
将它们传递到火鸟之前。

另外,从数据库中检索的任何字符数据(char,varchar,
blob sub_type文本)将升级到perl Unicode字符串。

警告:当前仅当 ib_charset dsn参数为 utf8 时才支持。将来,编码和解码到/
可以实现任意角色集。

示例:

  $ dbh = dbi-> connect('dbi:firebird:db = database.fdb; ib_chareet = utf8',
    {ib_enable_utf8 => 1});
 

As indicated by the link to the documentation of DBD::Firebird supplied by clamp in the comments, you need to connect using:

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

That is, the ib_charset property must be in the connection string, not in the hash, and its value must be UTF8 (not UTF-8!), and you need to add ib_enable_utf8 with setting 1 to the hash.

Specifically, this part:

ib_enable_utf8 (driver-specific, boolean)

Setting this attribute to TRUE will cause any Perl Unicode strings
supplied as statement parameters to be downgraded to octet sequences
before passing them to Firebird.

Also, any character data retrieved from the database (CHAR, VARCHAR,
BLOB sub_type TEXT) will be upgraded to Perl Unicode strings.

Caveat: Currently this is supported only if the ib_charset DSN parameter is UTF8. In the future, encoding and decoding to/from
arbitrary character set may be implemented.

Example:

$dbh = DBI->connect( 'dbi:Firebird:db=database.fdb;ib_charset=UTF8',
    { ib_enable_utf8 => 1 } );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文