Dear Gabor,
You must include «mysql_enable_utf8 => 1» hash record in the connection 
attributes used when you create the DBI handle
my %conn_attrs = (RaiseError    => $RaiseError,
                  PrintError    => $PrintError,
                  AutoCommit    => $AutoCommit,
                  mysql_enable_utf8 => 1);
my $dbh = DBI->connect 
         ($dsn, $user_name, $password, \%conn_attrs);

Meir


-----Original Message-----
From: szab...@gmail.com [mailto:szab...@gmail.com] On Behalf Of Gabor Szabo
Sent: יום ד 31 יולי 2013 19:09
To: dbi-users@perl.org
Subject: utf8 and mysql

Hi,

I am quite lost at this utf8 issue:
I have a file that I opened with

open(my $KEYWORDS, "<:encoding(UTF-8)", $keywords_file) as I read the rows I 
can print them to a log file (that was opened using open(my $fh, 
'>>:encoding(UTF-8)', $log_name) and it looks ok.
I insert it into the 'text' field of a table that was created like this:

CREATE TABLE terms (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    text VARCHAR(255) NOT NULL,
    language_id INT NOT NULL,
    UNIQUE(text, language_id)
) ENGINE=MyISAM CHARSET=utf8;

Then I read it back using SELECT and when I try to print it to the log file I 
get garbage there.

Calling decode('UTF-8', $t->{text}))  fixes the problem, but wonder if I should 
not get the data back correctly?

I tried connecting to mysql with two different connections strings:

"DBI:mysql:database=$attr->{database};host=$attr->{host};mysql_enable_utf8=1",

"DBI:mysql:database=$attr->{database};host=$attr->{host}",

and I tried to call

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

immediately after the connection was established, but neither helped.

Using perl 5.18 on OSX with DBI 1.627 DBD::mysql 4.023 MySQL itself is 5.6.10

Oh and if I connect to the database using the mysql client, it shows the text 
correctly.

your suggestions would be really appreciated

regards
    Gabor


-----
No virus found in this message.
Checked by AVG - www.avg.com
Version: 2013.0.3392 / Virus Database: 3209/6538 - Release Date: 07/31/13

Reply via email to