[Israel.pm] MySQL, Class::DBI and hebrew?

Shlomi Fish shlomif at iglu.org.il
Wed Mar 5 06:20:16 EET 2008


On Tuesday 04 March 2008, Shmuel Fomberg wrote:
> Hi All.
>
> I have a table in MySQL, that I access using Class::DBI, which contains
> names.

First of all a question - is there any reason you prefer Class::DBI over 
DBIx::Class which is:

1. Similar.

2. Newer.

3. Better.

4. Generally considered preferable - see 
http://www.perlmonks.org/?node_id=557402 .

===================

I was able to get DBIx::Class to work with Unicode by using the advice here: 
http://dev.mysql.com/tech-resources/articles/4.1/unicode.html and the 
following script:

{{{{{{{{{{{{{{{{{
#!/usr/bin/perl 

use strict;
use warnings;

package DB::Main::Artist;

use base qw/DBIx::Class/;

__PACKAGE__->load_components(qw/PK::Auto Core/);
__PACKAGE__->table('artist');
__PACKAGE__->add_columns(qw/ artistid name /);
__PACKAGE__->set_primary_key('artistid');

1;

package DB::Main;

use base qw/DBIx::Class::Schema/;

__PACKAGE__->load_classes("Artist");

1;

package main;

use utf8;

my $schema = DB::Main->connect("DBI:mysql:database=test_shmuel_hebrew");

my $artist = $schema->resultset("Artist")->create({name => "שמואל"});

my @all_results = $schema->resultset('Artist')->all();

# binmode STDOUT, ":utf8";
foreach my $artist (@all_results)
{
    print $artist->name(), "\n";
}
}}}}}}}}}}}}}}}}}}}}}}}

Here is my session of trial and error until I got it to work:

{{{{{{{{{{{{{{
shlomi:~$ cd progs/perl/
shlomi:~/progs/perl$ ls
3d         distros            gimp    old        POD-Revamp  snippets    XML
4Advent    Docs               gtk     osp_rules  Quizes      Subversion  yacc
arcs       du-s.txt           itself  p1.pl      Rindolf     test.txt
conv_cell  Extract-Method.pl  math    Parrot     sdl         v6
Core       file               mazes   patches    self-eval   Winapt
cpan       forums             net     PDL        server      www
shlomi:~/progs/perl$ cd snippets/
shlomi:~/progs/perl/snippets$ ls
a-b-regex.t                goedel-oo-test.pl     test.pl
analyse.pl                 implicit-self         test-state-vars.pl
apps-as-modules            io-file-test.pl       test-state-vars.pl~
attributes                 Module-Pluggable      test-subject.pl
Closure-as-Method          myfile.txt            timer.pl
delegate                   MyProxy               time-various-line-counts.pl
find-max-range.pl          Non-Instance- at ISA.pl  trap-stdout.pl
fractran.pl                test-amazon.pl        xmms-xchat-integration
function-call-backslash-G  test-fifo.pl
shlomi:~/progs/perl/snippets$ mkdir DBIx-Class-Hebrew
shlomi:~/progs/perl/snippets$ cd DBIx-Class-Hebrew/
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ sl
bash: sl: command not found
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ ls
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ ls
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ gvim dbix-class.pl
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ mysqladmin create 
test_shmuel_hebrew
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
DBIx::Class::Schema::resultset(): Can't find source for Artist at 
dbix-class.pl line 31
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ mysql test_shmuel_hebrew
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 270
Server version: 5.0.51a Mandriva Linux - MySQL Standard Edition (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> CREATE TABLE artist (artistid INT PRIMARY KEY AUTO_INCREMENT, name 
varchar(255));
Query OK, 0 rows affected (0.02 sec)

mysql> Bye
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
DBIx::Class::Schema::resultset(): Can't find source for Artist at 
dbix-class.pl line 31
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ mysql test_shmuel_hebrew
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 275
Server version: 5.0.51a Mandriva Linux - MySQL Standard Edition (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> CREATE TABLE artists (artistid INT PRIMARY KEY AUTO_INCREMENT, name 
varchar(255));
Query OK, 0 rows affected (0.04 sec)

mysql> Bye
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
DBIx::Class::Schema::resultset(): Can't find source for Artist at 
dbix-class.pl line 31
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl -d dbix-class.pl

Loading DB routines from perl5db.pl version 1.3
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

DB::Main::(dbix-class.pl:10):   __PACKAGE__->load_classes();
  DB<1> n
DB::Main::(dbix-class.pl:12):   1;
  DB<1> n
DB::Main::Artist::(dbix-class.pl:18):
18:     __PACKAGE__->load_components(qw/PK::Auto Core/);
  DB<1> n
DB::Main::Artist::(dbix-class.pl:19):
19:     __PACKAGE__->table('artist');
  DB<1> n
DB::Main::Artist::(dbix-class.pl:20):
20:     __PACKAGE__->add_columns(qw/ artistid name /);
  DB<1> n
DB::Main::Artist::(dbix-class.pl:21):
21:     __PACKAGE__->set_primary_key('artistid');
  DB<1> n
DB::Main::Artist::(dbix-class.pl:23):
23:     1;
  DB<1> n
main::(dbix-class.pl:29):       my $schema = 
DB::Main->connect("DBI:mysql:database=test_shmuel_hebrew");
  DB<1> n
main::(dbix-class.pl:31):       $schema->resultset("Artist")->new({name 
=> "שמואל"});
  DB<1> x $schema
0  DB::Main=HASH(0x86fb510)
   'storage' => DBIx::Class::Storage::DBI=HASH(0x84bf290)
      '_connect_info' => ARRAY(0x84bf5d0)
         0  'DBI:mysql:database=test_shmuel_hebrew'
      '_dbh_gen' => 0
      '_dbi_connect_info' => ARRAY(0x889f490)
         0  'DBI:mysql:database=test_shmuel_hebrew'
      '_in_dbh_do' => 0
      '_sql_maker' => undef
      '_sql_maker_opts' => HASH(0x8503e58)
           empty hash
      'cursor' => 'DBIx::Class::Storage::DBI::Cursor'
      'debugobj' => DBIx::Class::Storage::Statistics=HASH(0x88a6770)
         'debugfh' => IO::File=GLOB(0x88d3f78)
            -> *Symbol::GEN0
                  FileHandle({*Symbol::GEN0}) => fileno(3)
      'schema' => DB::Main=HASH(0x86fb510)
         -> REUSED_ADDRESS
      'transaction_depth' => 0
  DB<2> q
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
DBIx::Class::Schema::resultset(): Can't find source for Artist at 
dbix-class.pl line 31
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
DBIx::Class::Schema::resultset(): Can't find source for Artist at 
dbix-class.pl line 31
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
DBIx::Class::Schema::resultset(): Can't find source for Artist at 
dbix-class.pl line 32
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
Can't locate object method "source_name" via package "DB::Main::Artist" 
at /usr/lib/perl5/vendor_perl/5.8.8/DBIx/Class/Schema.pm line 300.
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
DBIx::Class::Schema::resultset(): Can't find source for Artist at 
dbix-class.pl line 31
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ mysql test_shmuel_hebrew
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 294
Server version: 5.0.51a Mandriva Linux - MySQL Standard Edition (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SELECT * FROM artist ;
Empty set (0.00 sec)

mysql> SELECT * FROM artists ;
Empty set (0.01 sec)

mysql> Bye
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
DBIx::Class::Relationship::CascadeActions::update(): Not in database at 
dbix-class.pl line 32
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl -d dbix-class.pl

Loading DB routines from perl5db.pl version 1.3
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

DB::Main::Artist::(dbix-class.pl:10):
10:     __PACKAGE__->load_components(qw/PK::Auto Core/);
  DB<1> c
 at /usr/lib/perl5/vendor_perl/5.8.8/DBIx/Class/Exception.pm line 53
        
DBIx::Class::Exception::throw('DBIx::Class::Exception', 'DBIx::Class::Exception=HASH(0x89926b0)', 
0) called at /usr/lib/perl5/vendor_perl/5.8.8/DBIx/Class/Schema.pm line 919
        
DBIx::Class::Schema::throw_exception('DB::Main=HASH(0x84bebc0)', 'DBIx::Class::Exception=HASH(0x89926b0)') 
called at /usr/lib/perl5/vendor_perl/5.8.8/DBIx/Class/Storage.pm line 125
        
DBIx::Class::Storage::throw_exception('DBIx::Class::Storage::DBI::mysql=HASH(0x84bee20)', 'DBIx::Class::Exception=HASH(0x89926b0)') 
called at /usr/lib/perl5/vendor_perl/5.8.8/DBIx/Class/Storage/DBI.pm line 625
        
DBIx::Class::Storage::DBI::txn_do('DBIx::Class::Storage::DBI::mysql=HASH(0x84bee20)', 'CODE(0x84bf250)') 
called at /usr/lib/perl5/vendor_perl/5.8.8/DBIx/Class/Schema.pm line 728
        
DBIx::Class::Schema::txn_do('DB::Main=HASH(0x84bebc0)', 'CODE(0x84bf250)') 
called at dbix-class.pl line 32
Debugged program terminated.  Use q to quit or R to restart,
  use o inhibit_exit to avoid stopping after program termination,
  h q, h R or h o to get additional info.
  DB<1> q
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
ש××××
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl | gvim -
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ Vim: Reading from stdin...

shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
שמואל
שמואל
שמואל
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ mysql test_shmuel_hebrew
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 315
Server version: 5.0.51a Mandriva Linux - MySQL Standard Edition (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SELECT * FROM artists ;
Empty set (0.00 sec)

mysql> SELECT * FROM artist ;
+----------+-----------------------+
| artistid | name                  |
+----------+-----------------------+
|        1 | שמו×ל            |
|        2 | שמו×ל            |
|        3 | שמו×ל            |
+----------+-----------------------+
3 rows in set (0.00 sec)

mysql> ALTER TABLE artist MODIFY name VARCHAR(255) CHARACTER SET utf8;
Query OK, 3 rows affected (0.03 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM artist ;
+----------+-----------------------+
| artistid | name                  |
+----------+-----------------------+
|        1 | שמו×ל            |
|        2 | שמו×ל            |
|        3 | שמו×ל            |
+----------+-----------------------+
3 rows in set (0.00 sec)

mysql> Bye
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ mysql test_shmuel_hebrew
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 319
Server version: 5.0.51a Mandriva Linux - MySQL Standard Edition (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> DELETE FROM artist ;
Query OK, 3 rows affected (0.01 sec)

mysql> Bye
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
שמואל
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
שמואל
שמואל
shlomi:~/progs/perl/snippets/DBIx-Class-Hebrew$ perl dbix-class.pl
שמואל
שמואל
שמואל
}}}}}}}}}}}}}}}}}}}}}}}}}}}}

Regards,

	Shlomi Fish

---------------------------------------------------------------------
Shlomi Fish      shlomif at iglu.org.il
Homepage:        http://www.shlomifish.org/

I'm not an actor - I just play one on T.V.


More information about the Perl mailing list