Utf-8 string truncated in MySQL table using Perl / DBI

I am trying to write utf-8 rows to a MySQL table using perl / DBI. For some reason, the string is truncated at the first character other than ascii.

For example, if I set up the following table:

CREATE DATABASE testdb DEFAULT CHARSET=utf8;
CREATE TABLE testdb.testtable (textval CHAR(30)) DEFAULT CHARSET=utf8;

And then run the following perl code:

#!/usr/bin/perl
use strict;
use DBI;
my $dbh = DBI->connect('DBI:mysql:host=localhost;database=testdb', 'testuser', 'somepassword', {mysql_enable_utf8 => 1}) or die $DBI::errstr;
$dbh->do('SET NAMES utf8');
$dbh->do("INSERT INTO testtable (textval) VALUES ('the N\xFCrburgring')");

He actually writes "N". (when he should write "Nurburgring")

In the MySQL query log, I see the following:

271 Query INSERT INTO testtable (textval) VALUES ('the Nürburgring')

Thus, the row reaches the database server unchanged.

If I enter the same query directly into the MySQL console:

INSERT INTO testtable (textval) VALUES ('the Nürburgring');

The entire line is spelled correctly. Any idea what I'm doing wrong?

+5
source share
1 answer

mysql_enable_utf8, , Perl. , Latin1.

use Devel::Peek qw(Dump);
Dump "the N\xfcrburgring";
#  FLAGS = (POK,READONLY,pPOK)
#  PV = 0x208e4f0 "the N\374rburgring"\0

. \x, utf8, Perl, UTF-8 UTF-8 ...

use utf8;
use Devel::Peek qw(Dump);
Dump "the Nürburgring";
#  FLAGS = (POK,READONLY,pPOK,UTF8)
#  PV = 0x20999f0 "the N\303\274rburgring"\0 [UTF8 "the N\x{fc}rburgring"]

... . , , , .

use Encode qw(decode);
use Devel::Peek qw(Dump);
Dump decode 'Latin1', "the N\xfcrburgring";
#  FLAGS = (TEMP,POK,pPOK,UTF8)
#  PV = 0x208f6b0 "the N\303\274rburgring"\0 [UTF8 "the N\x{fc}rburgring"]
+4

All Articles