2011-02-15 51 views
0

我正在使用Rose::DB::ObjectSQLite和中文文本。我的类看起来像这样:使用Rose :: DB :: Object处理SQLite中的UTF8编码文本

package My::DB; 

use base qw(Rose::DB); 

__PACKAGE__->use_private_registry; 

__PACKAGE__->register_db(
    driver => 'sqlite', 
    database => 'data/sqmple.db', 
); 

package Motorcycle; 

use My::DB; 

use base qw(Rose::DB::Object); 
... 
sub init_db { My::DB->new() }; 

用来存储一个记录中的代码:

Motorcycle->new(
    type => $self->param('type'), 
    brand => $self->param('brand'), 
    color => $self->param('color'), 
)->save; 

用于显示数据(从Mojolicious应用程序内)的代码:

<td><%= Mojo::ByteStream->new($cycle->type)->decode("utf-8") %></td> 
<td><%= Mojo::ByteStream->new($cycle->brand)->decode("utf-8") %></td> 
<td><%= Mojo::ByteStream->new($cycle->color)->decode("utf-8") %></td> 

我怎样才能消除解码步骤?我想显示的代码看起来像这个:

<td><%= $cycle->type %></td> 
<td><%= $cycle->brand %></td> 
<td><%= $cycle->color %></td> 

回答

0

我认为你需要得到sqlite_unicode => 1配置价值减记至SQLite的,有一个similar question约UTF8和SQLite,设置sqlite_unicode在那里做了诡计。

虽然我不认为Rose::DB::SQLite支持这个配置参数。基于this possibly similar issue with MySQL您可能能够通过增加这司机修补玫瑰:: DB :: SQLite的增加对sqlite_unicode支持:

sub sqlite_unicode { 
{ 
    shift->dbh_attribute_boolean('sqlite_unicode', @_) 
} 

我会离开约翰的回答评论,所以他能合法性检查这。

如果这样做的话,那么你可能需要给John Siracusa发送一个补丁(他不仅已经在这个问题上,还有Rose :: DB的维护者)。

0

如果你喂UTF8编码的文本到SQLite的,它应该给它的权利还给你以相同的形式。例如,假定有一个包含此架构的SQLite数据库名为test.db的:

CREATE TABLE things 
(
    id INTEGER PRIMARY KEY AUTOINCREMENT, 
    name VARCHAR(64) NOT NULL 
); 

运行这段代码的脚本在同一目录中test.db的数据库:

package My::DB; 

use base qw(Rose::DB); 

__PACKAGE__->use_private_registry; 

__PACKAGE__->register_db 
(
    driver => 'sqlite', 
    database => 'test.db', 
); 

package My::Thing; 

use base qw(Rose::DB::Object); 

__PACKAGE__->meta->setup 
(
    table => 'things', 
    columns => 
    [ 
    id => { type => 'serial', primary_key => 1, not_null => 1 }, 
    name => { type => 'text', length => 64, not_null => 1 }, 
    ], 
); 

sub init_db { My::DB->new } 

package main; 

# Set the name to a UTF8-encoded smiley: Unicode 0x263A 
my $thing = My::Thing->new(name => "\x{e2}\x{98}\x{ba}")->save; 

$thing = My::Thing->new(id => $thing->id)->load; 

# This will print the UTF8-encoded smiley; make sure your 
# terminal can handle UTF8 output. 
print $thing->name, "\n"; 

如果不是为您工作,那么也许您的电话获取表单参数(例如,$self->param('type'))正在返回字符串而不是UTF8编码的字符串。也就是说,在笑脸字符串的情况下,可能$self->param('foo')正在返回“\”而不是“\ x {e2} \ x {98​​} \ x {ba}”。在这种情况下,解决办法是设置对象之前编码字符串作为UTF8属性:

Motorcycle->new(
    type => utf8::encode($self->param('type')), 
    brand => utf8::encode($self->param('brand')), 
    color => utf8::encode($self->param('color')), 
)->save; 
+0

我不需要设置sqlite_unicode属性来让我的例子工作。也就是说,为Rose :: DB添加一个方法可能是值得的。我会添加到我的TODO。 – 2011-02-15 23:52:37