QuoteWenn ich die Daten jetzt wieder in die Datenbank schreiben möchte, und zwar utf-8-kodiert, muss ich die dann nochmal mittels encode() umwandeln?
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
#!perl use strict; use warnings; use utf8; use DBI; use Devel::Peek; use Encode; my $dbh = DBI->connect('DBI:mysql:database:server;mysql_enable_utf8=1', 'username', 'password'); die DBI::errstr() unless $dbh; my $stmt = qq~SELECT raw FROM files WHERE file_id = ?~; my $sth = $dbh->prepare($stmt); $sth->execute(1); my $row = $sth->fetchrow_hashref(); my $raw = $row->{raw}; Dump $raw; $raw = decode('utf8', $raw); Dump $raw; $raw = encode('utf8', $raw); Dump $raw; exit(0); __END__ SV = PV(0x35b7900) at 0x328d0f0 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x306df38 "\303\244\303\266\303\274\303\237kekse!\303\252"\0 CUR = 16 LEN = 24 SV = PVMG(0x3539908) at 0x328d0f0 REFCNT = 1 FLAGS = (PADMY,POK,pPOK,UTF8) IV = 0 NV = 0 PV = 0x306bbb8 "\303\244\303\266\303\274\303\237kekse!\303\252"\0 [UTF8 "\x{e4}\x{f6}\x{fc}\x{df}kekse!\x{ea}"] CUR = 16 LEN = 24 SV = PVMG(0x3539908) at 0x328d0f0 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) IV = 0 NV = 0 PV = 0x306bb78 "\303\244\303\266\303\274\303\237kekse!\303\252"\0 CUR = 16 LEN = 24
2012-02-23T18:08:19 pqich glaube, du musst mysql_enable_utf8 als attribut übergeben, nicht im DSN.
vielleicht geht das auch im DSN, aber das habe ich noch nie ausporobiert. schau mal, ob das einen unterschied macht.
1 2 3 4 5
{ RaiseError => 1, mysql_enable_utf8 => 1, # ab DBD Version ?? musst gucken, sorry ;) PrintError => 0, }
create ..()default charset utf8
2012-02-23T18:44:21 rostiEs kommt in die Hashref der Attribute, neben RaiseError, PrintError, AutoCommit...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
SV = PV(0x35f7d20) at 0x32cd0a8 REFCNT = 1 FLAGS = (PADMY,POK,pPOK,UTF8) PV = 0x30ade38 "\303\244\303\266\303\274\303\237kekse!\303\252"\0 [UTF8 "\x{e4}\x{f6}\x{fc}\x{df}kekse!\x{ea}"] CUR = 16 LEN = 24 SV = PVMG(0x357a958) at 0x32cd0a8 REFCNT = 1 FLAGS = (PADMY,POK,pPOK,UTF8) IV = 0 NV = 0 PV = 0x30a86f8 "\357\277\275\357\277\275\357\277\275\357\277\275kekse!\357\277\275"\0 [UTF8 "\x{fffd}\x{fffd}\x{fffd}\x{fffd}kekse!\x{fffd}"] CUR = 21 LEN = 24 SV = PVMG(0x357a958) at 0x32cd0a8 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) IV = 0 NV = 0 PV = 0x30ab9f8 "\357\277\275\357\277\275\357\277\275\357\277\275kekse!\357\277\275"\0 CUR = 21 LEN = 24
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
#!perl use strict; use warnings; use utf8; use FindBin qw/$Bin/; use Data::Dumper qw/Dumper/; use Text::CSV; use DBI; use SQL::Abstract; my $dbh = DBI->connect('DBI:mysql:ling:127.0.0.1;mysql_enable_utf8=1', 'test', 'test'); die DBI::errstr() unless $dbh; my $file = 'source.csv'; my $csv = Text::CSV->new ({ sep_char => ';', empty_is_undef => 1, binary => 1, }) or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, "<:encoding(utf8)", $file or die "$file: $!"; while ( my $row = $csv->getline( $fh ) ) { # Absetzen des Import-Statements für 1 Zeile hier } $csv->eof or $csv->error_diag(); close $fh; exit(0);
open my $fh, "<:encoding(utf8)", $file or die "$file: $!";
$DBH->do("SET NAMES UTF8");