gpt4 book ai didi

mysql - 为什么汉字在MySQL和Perl中显示异常?

转载 作者:行者123 更新时间:2023-11-29 04:13:08 25 4
gpt4 key购买 nike

我使用 Perl ORM Rose::DB::Object 和 MySQL 表/列排序规则 utf8_general_ci。保存数据代码:

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

Mojolicious中获取数据的代码(代码等同于utf8::decode):

<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>

中文字符在我的应用程序中看起来不错,但在 phpMyAdmin 中却显得很奇怪。如果我将字符保存在 phpMyAdmin 中,它们在 phpMyAdmin 中会很好,但在我的应用程序中看起来很奇怪。

我已经在 SQLite 和 Firefox SQLite 附加组件中对其进行了测试,字符在 Firefox SQLite 附加组件和我的应用程序中看起来都很好。

我认为这是服务器问题。如果我将 Perl 与 PHP 融合,它可能会成为一场灾难。

感谢您的帮助。


解决如下:

__PACKAGE__->use_private_registry;
__PACKAGE__->default_connect_options( mysql_enable_utf8 => 1 );
__PACKAGE__->register_db(
driver => 'mysql',
database => 'test',
host => 'localhost',
username => 'root',
password => '',

添加这一行很重要:

__PACKAGE__->default_connect_options( mysql_enable_utf8 => 1 );

读回数据时无需解码。

最佳答案

在 Perl 代码中,您需要确保使用 Encode::decode() 输入,以便您的数据采用 perl 的内部字符表示形式,处理您的数据,然后在写入数据库之前使用 Encode::encode() 输出。您可能还想这样做:

$dbh->do('SET NAMES "utf8"');

连接到数据库后,MySQL 客户端库和服务器就字符集达成一致。

参见:http://perldoc.perl.org/perlunitut.htmlhttp://perldoc.perl.org/perlunicode.html

关于mysql - 为什么汉字在MySQL和Perl中显示异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4998858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com