Thread Mysql und Perl (34 answers)
Opened by Johannes at 2012-06-14 09:14

topeg
 2012-06-14 15:49
#159029 #159029
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Ich denke so gefällt es euch:
more (25.1kb):
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
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
package         database                ;
use             strict          ;
use             warnings                ;
use             DBI             ;

our             $con                    =               "DBI:mysql:test_db"             ;
our             $user                   =               "root"          ;
our             $passwort               =               "pwd"           ;
my              $db             ;
                
sub             open_db         (               )               {
                $db             =               DBI             ->              connect         (               $con            ,               $user           ,               $passwort               )               or              die             "DB             connection              not             made:           $DBI::errstr"           unless          $db             ;
                return          $db             ;
}

sub             close_db                (               )               {
                return          unless          $db             ;
                $db             ->              disconnect              ;
                undef           $db             ;
}

sub             insert_tab              {
                my              $table                  =               shift           ;
                my              $values                 =               shift           ;
                my              $primary_key            =               shift           ;
                #               $table          und             $values         müssen         vorhanden               sein!
                return          0               unless          $table          and             $values;

                #               $values         muss            ein             hash            sein!
                return          0               if              ref             $values         ne              'HASH'          ;

                #               INSERT          -----------

                #               Keys            und             Values          separieren
                my              @vals           ;
                my              @keys           ;
                while(          my              (               $k              ,               $v              )                               =               each            %$values                )               {
                                push            @vals           ,               $k              ;
                                push            @keys           ,               $v              ;
                }
                my              $insert_keys            =               join            ',              '               ,               @keys           ;

                #               Für            jeden           wert            ein             '?'
                my              $insert_vals            =               join            ',              '               ,               '?'             x               @keys;

                my              $dbh            =               open_db         ;

                my              $sth            =               $dbh            ->              prepare         (               qq              !INSERT         INTO            $table          (               $insert_keys            )               VALUES          (               $insert_vals            )               !               )               ;
                my              $ok             =               $sth            ->              execute         (               @$values                )               ;
                $sth            ->              finish          ;

                #               einfügen               Erfolgreich
                return          1               if              $ok             ;

                #               einfügen               war             nicht           erfolgreich             aber            kein            Primärer               Key             wurde           definiert
                return          0               unless          defined         $primary_key            ;

                #               UPDATE          ---------

                #               ist             der             Primäre                Key             aufgeführt?
                return          0               unless          exists          $values         ->              {               $primary_key            }               ;

                #               primären               key             entfernen
                my              $primary_val            =               delete          $values         ->              {               $primary_key            }               ;

                #               Keys            und             Values          separieren
                @vals           =               (               )               ;
                @keys           =               (               )               ;
                while(          my              (               $k              ,               $v              )               =               each            %$values                )               {
                                push            @vals           ,               $k;
                                push            @keys           ,               $v;
                }
                my              $update_vals            =               join            ',              '               ,               map             {               "$_             =               ?"              }               @keys;

                $sth            =               $dbh            ->              prepare         (               qq              !update         $table          SET             $update_vals            WHERE           $primary_key            =               ?!              )               ;
                $ok             =               $sth            ->              execute         (               @vals           ,               $primary_val            )               ;
                $sth            ->              finish          ;

                return          $ok;
}

1               ;
:)

View full thread Mysql und Perl