Hackerboard Wiki HaboBlog
Hackerboard bei Facebook Hackerboard bei Google+ Hackerboard bei Twitter

[HaBo]

 
(Web-) Design und webbasierte Sprachen Tipps & Tricks, Designabgleich, HTML & Javascript, Flash, ASP, PHP, Perl/CGI...

DBI - "fetch() without execute()"

Diskussion: DBI - "fetch() without execute()" im Forum (Web-) Design und webbasierte Sprachen, in der Kategorie Web, Network & Multimedia Palace; Anzeige Hoi, ich versuche gerade eine Klasse zur einfacheren Handhabung von MySQL-Befehlen zu schreiben: alter Beitrag     code   ...

Antwort
Alt 17.02.08, 22:59   #1 (permalink)
 
Registriert seit: 06.01.07
keksinat0r Leistung: Facit NTK
Likes: 0
DBI - "fetch() without execute()"

Anzeige

Hoi, ich versuche gerade eine Klasse zur einfacheren Handhabung von MySQL-Befehlen zu schreiben:

alter Beitrag   

code   
Code:
package MySQL;
  use strict;
  use warnings;

  use DBI;


  sub new {

    my $class = shift;
    my $self = {};
    my %ARG = @_;

    (defined $ARG{'-username'} and my $USERNAME = $ARG{'-username'})
    or die "You have to set the username to use";

    (defined $ARG{'-password'} and my $PASSWORD = $ARG{'-password'})
    or die "You have to set the password to use";

    (defined $ARG{'-database'} and my $DATABASE = $ARG{'-database'})
    or die "You have to set the database to use";

    (defined $ARG{'-hostname'} and my $HOSTNAME = $ARG{'-hostname'})
    or die "You have to set the hostname to use";

    (defined $ARG{'-hostport'} and my $HOSTPORT = $ARG{'-hostport'})
    or die "You have to set the hostport to use";

    ($self->{DBH} = DBI -> connect("DBI:mysql:database=$DATABASE;host=$HOSTNAME;port=$HOSTPORT",$USERNAME,$PASSWORD))
    or die "Unable to connect to database";

    bless( $self, $class );
    return $self;

  }

[...]

  sub fetchrow_array {

    my $self = shift;

    my $CMD = $self->{DBH} -> prepare($_[0]);
    unless($DBI::err && $DBI::errstr){
      $CMD -> execute();
      unless($DBI::err||$DBI::errstr){
        my @RESULT;
        my $SUCCESS = 1;
        while(my $row = [ $CMD -> fetchrow_array ]){
          unless($DBI::err||$DBI::errstr){
            push(@RESULT, $row);
          }else{
            $SUCCESS = 0;
            last;
          }
        }
        if( $SUCCESS ){
          return @RESULT;
        }else{
          warn "MySQL error: [$DBI::err] '$DBI::errstr'";
          return 0
        }
      }else{
        warn "MySQL error: [$DBI::err] '$DBI::errstr'";
        return 0
      }
    }else{
      warn "MySQL error: [$DBI::err] '$DBI::errstr'";
      return 0
    }

  }

1;


wenn ich jetzt aber
Code:
 (MySQL -> new( -username => ... )) -> fetchrow_array( "SELECT ..." )
aufrufe, spuckt er mir nur "fetch() without execute()" aus...

jemand eine Ahnung warum?


Hat sich erledigt.
Keine Ahnung warum es nicht funktioniert hat, aber folgende Loesung funktioniert einwandfrei:

code   
Code:
package MySQL;

  use 5.008;
  use strict;
  use warnings;

  require DBI;




  sub new {

    my $class = shift;
    my $self = {};
    my %ARG = @_;

    (defined $ARG{'-username'} and my $USERNAME = $ARG{'-username'})
    or die "You have to set the username to use";
    (defined $ARG{'-password'} and my $PASSWORD = $ARG{'-password'})
    or die "You have to set the password to use";
    (defined $ARG{'-database'} and my $DATABASE = $ARG{'-database'})
    or die "You have to set the database to use";
    (defined $ARG{'-hostname'} and my $HOSTNAME = $ARG{'-hostname'})
    or die "You have to set the hostname to use";
    (defined $ARG{'-hostport'} and my $HOSTPORT = $ARG{'-hostport'})
    or die "You have to set the hostport to use";

    my $DBH = DBI -> connect( "DBI:mysql:database=$DATABASE;host=$HOSTNAME;port=$HOSTPORT",$USERNAME,$PASSWORD )
    or die "Unable to connect to database";
    $self->{DBH} = \$DBH;

    bless( $self, $class );
    return $self;

  }

[...]

  sub fetchrow_array {

    my $self       = shift;
    my $DBHandle   = ${$self->{DBH}};
    my $SQLCommand = $_[0];
    my $SQLHandle  = undef;
    my @Result     = undef;
    my @Output     = ();


    $SQLHandle = $DBHandle -> prepare( $SQLCommand ) and not $DBI::err
    or die "Unable to prepare SQL command '$SQLCommand'";

    $SQLHandle -> execute and not $DBI::err
    or die "Unable to execute SQL command '$SQLCommand'";

    while( 1 ){
      @Result = $SQLHandle -> fetchrow and not $DBI::err or last;
      push( @Output, \@Result );
    }

    return @Output;

  }

1;

MFG - Keks :)
keksinat0r ist offline   Mit Zitat antworten
Antwort
   
- Anzeige -

Werbung ist gerade online    

[HaBo] » Web, Network & Multimedia Palace » (Web-) Design und webbasierte Sprachen » DBI - "fetch() without execute()"
Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks sind aus
Pingbacks sind aus
Refbacks sind aus


Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
Instruction Fetch Unit woofast Hardware Probleme 3 22.01.09 15:28
Execute ausführen kr4nk$ Mac OS & Co. 2 14.09.07 21:59


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