can not use "set names 'utf8'" in mysql and multi-bytes data

33 views
Skip to first unread message

silent

unread,
Nov 23, 2011, 9:02:02 PM11/23/11
to rose-db-object
after do "set names 'utf8'" in mysql,
the data length increased, and load() die,

my test detail:


mysql> show create table test;
Table,Create Table
'test','CREATE TABLE `test` ( `id` int(11) NOT NULL, `text`
varchar(5) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT
CHARSET=utf8'

# and I insert a row with a Chinese words( 5 chinese characters) as 'text'.


#!/usr/bin/perl

use Rose::DB::Object::Loader;

my $loader = Rose::DB::Object::Loader->new(
db_dsn => 'dbi:mysql:dbname=test;host=127.0.0.1',
db_username => "root",
db_password => "wodemysqlmima",
db_options => { AutoCommit => 1, ChopBlanks => 1, RaiseError => 1 },
class_prefix => 'My'
);


$loader->make_modules;
##


############ My::Test

package My::Test;

use strict;

use base qw(My::DB::Object::AutoBase2);

__PACKAGE__->meta->setup(
table => 'test',

columns => [
id => { type => 'integer', not_null => 1 },
text => { type => 'varchar', length => 5 },
],

primary_key_columns => [ 'id' ],
);

1;

######## autobase2

package My::DB::Object::AutoBase2;

use base 'Rose::DB::Object';

use My::DB::AutoBase1;

my $db = My::DB::AutoBase1->new;
$db->dbh->do("set names 'utf8'"); ### added by me

sub init_db { $db }

1;


###### test.pl

use lib ".";

use My::Test;
use My::Test::Manager;
use Encode qw(encode decode);

#my $test = My::Test::Manager->get_test();
my $test = My::Test->new( id => 1 );
$test->load();
print $test->text, "\n";


#### result:

load() - My::Test: Value for text() is too long. Maximum length is 5
character. Value is 15 characters:

is this a bug ?

John Siracusa

unread,
Nov 28, 2011, 12:21:40 PM11/28/11
to rose-db...@googlegroups.com
You should set mysql_enable_utf8 => 1 in your connect_options in your
Rose::DB-derived database connection class:

http://search.cpan.org/dist/Rose-DB/lib/Rose/DB/MySQL.pm#mysql_enable_utf8

The load() call in your example works when I do that.

-John

silent

unread,
Dec 1, 2011, 8:21:31 AM12/1/11
to rose-db...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages