DBI, Mysql & Unicode

angie ahl <[email protected]>
Newsgroups gmane.comp.db.mysql.perl
Message-ID <[email protected]>
Hi List

Please excuse the cross posting, I've also posted on the Perl DBI
list, but no one there seems to be able to help (or they're all
recovering from thanksgiving or something ;)

I've been using DBI & MySQL for some time now and have decided to try
and use unicode so that my web apps can be multilingual.

I'm trying to work out getting data into and out of MySQL with utf 8.

I'm inserting the data like this:

I've got a hash in the following format:

my %uni = (
       hebrew_alef => {
                       character => chr(0x05d0),
                       language => "hebrew",
       },
       recenu => {
                       character => "re\x{e7}enu",
                       language => "french",
       },
);

and I'm inserting the values into the database like this:

my $funny = "CONVERT(_utf8'$ucode' USING utf8)";
my $sql = qq {INSERT INTO unitest (id, aword) VALUES ( "$id", $funny )};

Is this CONVERT business necessary/the right way to do it?.

getting the data back is done like this:

sub dbget {
       my $id = shift;
       my $sql = "select aword from unitest where id = \"$id\"";
       my $cur = $dbh->prepare($sql);
       $cur->execute;
       my $char = ($cur->fetchrow)[0];
       return decode("utf8", $char);
}

running is_utf8( &dbget($_)) ? "is unicode" : "is not unicode";
indicates that I am getting utf8 data back when I use decode, but
here's what's wrong:

the following code is used to output the data:

foreach (sort keys %uni) {
       &dbpush( $_, $uni{$_}->{character} );
       printf $tablinedef,
               $_,
               $uni{$_}->{language},
               $uni{$_}->{character},
               &dbget( $_ ),
               is_utf8( &dbget($_)) ? "is unicode" : "is not unicode";
}

$uni{$_}->{character} shows the character as expected in firefox eg: abîmer

but &dbget doesn't show it correctly (it should apparently), but the
is_utf8 test says it is utf8.

Sorry for the long post but I'm so new to unicode that I just can't
work out what I'm missing.... Here's hoping Mr Dubois is around....
great books BTW thanks.

Angie

-- 
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe:    http://lists.mysql.com/[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.