如何处理perl中的utf字符

时间:2013-08-07 06:51:50

标签: mysql perl

我在Perl中获取数据库表字段值(即'utf8_unicode_ci')并尝试将该值插入另一个表(也是'utf8_unicode_ci');

但我没有在perl中正确获得该值。

我尝试阅读和插入的值是

我尝试了不同的解决方案,但它们无法正常工作

任何人都可以帮忙吗?

我尝试过以下代码:

use Encode; $message = decode_utf8( $message );

2 个答案:

答案 0 :(得分:2)

您可能需要将此添加到连接中:

my $dbh = DBI->connect(...);
$dbh->do("SET NAMES 'utf8'");  #<-- add this after the connection was established

使用SET NAMES 'utf8',我们用一句话来做:

SET character_set_client = 'UTF8';
SET character_set_results = 'UTF8';
SET character_set_connection = 'UTF8';

MySQL documentation可能会有所帮助。

答案 1 :(得分:1)

如果您在连接数据库时使用mysql_enable_utf8属性,那么DBD::mysql将为您进行转换。

my $dbh = DBI->connect("dbi:mysql:database=$db", $user, $pass",
                       { mysql_enable_utf8 => 1 });
相关问题