0

libSqliteIcu在sqlite3 终端工作正常:

sqlite3
...
.load './libSqliteIcu.so'
SELECT icu_load_collation('pl_PL', 'POLISH');
SELECT DISTINCT miasto FROM tab ORDER BY miasto COLLATE POLISH;

如何将上面的序列编码到perl中?

4

2 回答 2

1

未经测试:

use DBI qw();
my $dbh = DBI->connect('DBI:SQLite:thedatabase.sqlite');
$dbh->sqlite_enable_load_extension(1);
$dbh->do(q{SELECT load_extension('./libSqliteIcu.so')});
$dbh->do(q{SELECT icu_load_collation('pl_PL', 'POLISH')};
my $miasto = $dbh->selectall_arrayref(q{SELECT DISTINCT miasto FROM tab ORDER BY miasto COLLATE POLISH});


或者,在 Perl 中排序:

use utf8;
my @tongue_twister = qw(Król Karol kupił królowej Karolinie korale koloru koralowego);

use Unicode::ICU::Collator qw();    # ICU C library, fast
my $c = Unicode::ICU::Collator->new('pl_PL');

use Unicode::Collate qw();          # pure-perl, slow
my $c = Unicode::Collate->new(locale => 'pl_PL');

my @sorted = $c->sort(@tongue_twister);
# (
#     'Karol',
#     'Karolinie',
#     'koloru',
#     'korale',
#     'koralowego',
#     'Król',
#     'królowej',
#     'kupił'
# )
于 2012-06-08T13:03:01.147 回答
0

一种简单但不完美的解决方案是使用pl_PL.UTF8语言环境。

sqlite3我们准备:

create table A(idA);
...
insert into A values('ź');
insert into A values('Ż');
insert into A values('ź');
insert into A values('Š');
...
insert into A values('ś');
...

现在:

select * from A;

给出:Z ź Ż ź z ś ż S Š z

所以我们加载:

.load './libSqliteIcu.so'
SELECT icu_load_collation('pl_PL', 'POLISH');
select idA from A order by idA collate POLISH;

我们有:S Š ś zz Z ź ź ż Ż

所以现在在perl中我们可以输入:

SELECT idA FROM A ORDER BY idA

并得到不正确的顺序:SZ zz ś Š ź ź Ż ż

现在,使用perllocale我们输入perl

SELECT idA FROM A ORDER BY idA COLLATE perllocale

并得到订单S Š ś zz Z ź ź ż Ż

在我的情况下$dbh->do(q{SELECT load_extension('./libSqliteIcu.so')});不起作用;它会出错:

DBD::SQLite::db do failed: 由于初始化期间活动语句错误,无法删除/修改用户函数

于 2012-06-08T20:31:00.997 回答