Leser: 27
1
2
3
4
5
6
7
Nr | Old | New | Length
10 | AAA| Art | 100
20 | BBB | Brf | 25
30 | CCC |Txt | 35
40 | DDD |Art | 200
50 | EEE | | 255
60 | FFF | Txt | 150
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
my $search = $dbh->prepare("SELECT * FROM tblimport"); $search->execute; my (@rec,@news); while(@rec = $search->fetchrow_array){ my @array = join("\t",@rec); foreach(@array){ my ($id,$old,$new,$length,); if(/^([^\t]+)\t([^\t]+)\t([^\t]*)\t([^\t]+)$/){ $new = $3; if($new){ push @news, $_; } } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# hole alles aus der Datenbank my $search = $dbh->prepare("SELECT * FROM tblimport"); $search->execute; my %new; # hole jede Zeile als Hashreferenz, damit ueber den Namen zugegriffen werden kann while ( my %set = $search->fetchrow_hashref ) { my $key = $set->{New}; # hole das, was in der Spalte "New" steht # schiebe das aus der Spalte "Old" in ein Array push @{ $new{$key}->{strings} }, $set->{Old}; # addiere die Länge $new{$key}->{length} += $set->{Length}; } # gib alles aus for my $key ( keys %new ) { my $all_strings = join " ", @{ $new{$key}->{strings} }; my $length = $new{$key}->{length}; print "$key\t$all_strings\t$length\n"; }
while ( my $set = $search->fetchrow_hashref ) {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
my @anzahl; for my $key ( keys %new ) { my $all_strings = join " ", @{ $new{$key}->{strings} }; my $length = $new{$key}->{length}; print "$key\t$all_strings\t$length\n"; push @anzahl, $key; } if($#anzahl == 0){ $insertvalue = qq!insert into daten ($anzahl[0]) value(?)!; my $sth = $dbh->prepare($insertvalue); $sth->execute(...); } elsif($#anzahl == 1){ $insertvalue = qq!insert into daten ($anzahl[0],$anzahl[1]) value(?,?)!; my $sth = $dbh->prepare($insertvalue); $sth->execute(...); } elsif($#anzahl == 2){ $insertvalue = qq!insert into daten ($anzahl[0],$anzahl[1],$anzahl[2]) value(?,?,?)!; my $sth = $dbh->prepare($insertvalue); $sth->execute(...); } ...
1 2 3 4 5 6 7 8
for my $key ( keys %new ) { my $all_strings = join " ", @{ $new{$key}->{strings} }; my $length = $new{$key}->{length}; print "$key\t$all_strings\t$length\n"; my $insert_sth = $dbh->prepare( 'INSERT INTO daten (New, Merge, Length) VALUES( ?, ?, ? )' ); $insert_sth->execute( $key, $all_strings, $length ); }
1
2
3
4
AAA | BBB | CCC | DDD | EEE | FFF
aaa | bbb | ccc | ddd | eee | fff
aaa | bbb | ccc | ddd | eee | fff
aaa | bbb | ccc | ddd | eee | fff
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
my @anzahl; my $tableinhalt = qq!CREATE TABLE daten(!; for my $key ( keys %new ) { my $all_strings = join " ", @{ $new{$key}->{strings} }; my $length = $new{$key}->{laenge}; next unless $key; push @anzahl, $key; $tableinhalt .= qq!$key varchar ($length) NOT NULL,!; } $tableinhalt .= ')'; $tableinhalt =~ s/,\)/\)/; my $dbtable = $dbh->prepare($tableinhalt); $dbtable->execute; if($#anzahl == 0){ $insertvalue = qq!insert into daten ($anzahl[0]) value(?)!; my $sth = $dbh->prepare($insertvalue); $sth->execute(...); } elsif($#anzahl == 1){ $insertvalue = qq!insert into daten ($anzahl[0],$anzahl[1]) value(?,?)!; my $sth = $dbh->prepare($insertvalue); $sth->execute(...); } elsif($#anzahl == 2){ $insertvalue = qq!insert into daten ($anzahl[0],$anzahl[1],$anzahl[2]) value(?,?,?)!; my $sth = $dbh->prepare($insertvalue); $sth->execute(...); } ...
1 2 3 4 5 6 7 8
die("keine Feldliste") if @anzahl <= 0; my $insertvalue = "insert into daten (" . join(",", @anzahl) . ") values (" . join(",", ("?") x @anzahl) . ")"; my $sth = $dbh->prepare($insertvalue); $sth->execute(...);