Thread utf-8-Daten aus DB an Skript senden und richtig ausgeben
(15 answers)
Opened by
pktm
at 2009-10-05 13:50
User since 2003-08-07
2921
Artikel
BenutzerIn
Also, hier mal ein Testskript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
#!/usr/bin/perl -w
package MyTest;
use strict; use warnings; use DBI; use Devel::Peek; use Data::Dumper qw/Dumper/; use Perl6::Say; use FindBin qw/$Bin/; use lib $Bin;
use Test::Schema;
=head2 new( $cfg_file )
=cut
sub new { my $class = shift; my $self = bless({}, $class); $self->{'__CONFIG'} = { db => { dsn => 'DBI:mysql:test:localhost;mysql_enable_utf8=1', username => 'test', password => 'test', attributes => { autocommit => 1, }, }, # /db }; return $self; } # /mew
=head2 cfg( $key? )
Reads the value for $key from the config file. If $key is omitted, the config hash is returned at once.
=cut
sub cfg { my $self = shift; my $key = shift; # may be undef if( $key ) { return $self->{'__CONFIG'}->{$key}; }else{ return $self->{'__CONFIG'}; } } # /cfg
=head2 schema()
Connect to a schema. Anything else than the authentication credentials is hardcoded :-)
=cut
sub schema { my $self = shift; my $schema1 = $self->{'__SCHEMA'}; unless( $schema1 ) { my $dbc = $self->cfg('db') or die("Missing configuration values: db connection attributes"); $schema1 = Test::Schema->connect( $dbc->{dsn}, $dbc->{username}, $dbc->{password}, $dbc->{attributes}, ); $schema1->storage->sql_maker->quote_char('`'); $schema1->storage->sql_maker->name_sep('.'); $self->{'__SCHEMA'} = $schema1; }
return $schema1; } # /schema
=head2 with_dbic()
Get a word with special chars from test database using DBIx::Class.
=cut
sub with_dbic { my $self = shift;
my $schema = $self->schema(); my $word = $schema->resultset('Words')->first()->word(); say STDERR $word; Devel::Peek::Dump($word); } # /run
=head2 with_dbi()
Get a word with special chars from test database using DBI.
=cut
sub with_dbi { my $self = shift; my $dbc = $self->cfg('db') or die("Missing configuration values: db connection attributes"); my $dbh = DBI->connect( $dbc->{dsn}, $dbc->{username}, $dbc->{password}, $dbc->{attributes}, ) or die("Could not connect to DB " . DBI->errstr()); my $sql = qq~ SELECT word FROM words LIMIT 1 ~; my $sth = $dbh->prepare($sql) or die("prep: " . $dbh->errstr()); my $rv = $sth->execute() or die("exec: " . $dbh->errstr()); while( my $href = $sth->fetchrow_hashref() ) { my $word = $href->{word}; say STDERR $word; Devel::Peek::Dump( $word ); } } # /with_dbi
1;
use strict; use warnings; use FindBin qw/$Bin/; use lib $Bin;
my $app = MyTest->new(); $app->with_dbic(); $app->with_dbi();
Hier die Tabelle dazu:
CREATE TABLE IF NOT EXISTS `words` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `word` varchar(45) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
-- -- Daten für Tabelle `words` --
INSERT INTO `words` (`id`, `word`) VALUES (1, 'Größe');
und Test::Schema::Words.pm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
package Test::Schema::Words;
use strict; use warnings;
use base 'DBIx::Class';
__PACKAGE__->load_components("Core"); __PACKAGE__->table("words"); __PACKAGE__->add_columns( "id", { data_type => "INT", default_value => undef, is_nullable => 0, size => 10 }, "word", { data_type => "VARCHAR", default_value => undef, is_nullable => 0, size => 45, }, ); __PACKAGE__->set_primary_key("id");
# Created by DBIx::Class::Schema::Loader v0.04006 @ 2009-10-05 15:47:42 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:5dRfU5LHHx2UY5/NtWqqGQ
# You can replace this text with custom content, and it will be preserved on regeneration 1;
Selbstverständlich funktioniert hier alles prima -.-
Meine Ausgabe (für das Beispiel "Größe"):
Größe SV = PVMG(0xbed900) at 0xb0a988 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) IV = 0 NV = 0 PV = 0x159e3f0 "Gr\303\266\303\237e"\0 CUR = 7 LEN = 8 Größe SV = PV(0xb5a500) at 0xb151d8 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x15a0870 "Gr\303\266\303\237e"\0 CUR = 7 LEN = 8
Aber: wenn ich jetzt die selbe Sache auf einer anderen Datenbasis laufen lasse, dann kommt:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Blutdruckmessger‰t SV = PVMG(0xc81140) at 0xba1bc0 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK,UTF8) IV = 0 NV = 0 PV = 0x1682050 "Blutdruckmessger\303\244t"\0 [UTF8 "Blutdruckmessger\x{e4}t"] CUR = 19 LEN = 20 MAGIC = 0x1680ff0 MG_VIRTUAL = &PL_vtbl_utf8 MG_TYPE = PERL_MAGIC_utf8(w) MG_LEN = 18 Blutdruckmessgerät SV = PV(0xc005ac) at 0xba20dc REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x1680a70 "Blutdruckmessger\303\244t"\0 CUR = 19 LEN = 20
Kann ich irgendwie mit einem SQL-Befehl die Zeichenkodierung von Daten überprüfen lassen?
View full thread utf-8-Daten aus DB an Skript senden und richtig ausgeben
|