Thread DBD::SQLite - Anzahl Zeilen abfragen (21 answers)
Opened by Kean at 2011-05-09 09:16

rosti
 2011-05-11 19:59
#148478 #148478
User since
2011-03-19
3199 Artikel
BenutzerIn
[Homepage]
user image
hi again,

war dochn schöner Tach heute ;)

hab grad eben noch eine kleine Klasse geschrieben, wo ein hash gebunden wird, im Hintergrund wird eine Verbindung zur DB/Unicode-Tabelle hergestellt. Diese Tabelle hat als Inhalt die Felder Codepoint und Name, Beispiel:

Codepoint: 181 Name: MICRO SIGN

Grundlage für die Tabelle ist die Datei UnicodeData.txt vom Unicode Konsortium (Download: Suche in Suchmaschine, Suchbegriff Dateiname).

Das Modul findet hier Verwendung: http://rolfrost.de/apps/codepoint

Und hier isses:

Code (perl): (dl )
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
package Unicode;

use myConfig qw($cfg);
use strict;
use DBI;

# Konstruktor, DB-Connect
sub new{
        my $class = shift;
        my $self = {
                DBH => undef,
        };
        bless $self, $class;
        my $dsn = "DBI:mysql:database=$cfg->{mysql}->{base};host=$cfg->{mysql}->{host};port=$cfg->{mysql}->{port}";    
        eval{
                $self->{DBH} = DBI->connect_cached(
                        $dsn, $cfg->{mysql}->{user}, $cfg->{mysql}->{pass}, {RaiseError => 1, PrintError => 0}
                );
        };
        return $self;
}

# hier wird nur das Objekt zurückgegeben
sub TIEHASH{
        my $class = shift;
        my $self = $class->new;
        if($@){ return }
        else{ return $self}
}

# erst hier werden die Daten aus der mySQL-Tabelle geholt
sub FETCH{
        my $self = shift;
        my $key = shift; # Codepoint decimal, cpd
        my $q = q(SELECT cpd, name FROM unicode WHERE cpd=?);
        my $href = {};
        eval{
                my $sth = $self->{DBH}->prepare($q);
                $sth->execute($key);
                $href = $sth->fetchall_hashref('cpd');
        };
        if($@){ return "Fehler: $@" }
        else{ return $href->{$key}->{name} }
}
1;#########################################################################


package main;
tie(my %h, 'Unicode') or die $@;
print $h{8364}; # EURO SIGN

Last edited: 2011-05-11 20:02:20 +0200 (CEST)

View full thread DBD::SQLite - Anzahl Zeilen abfragen