Hoi, ich versuche gerade eine Klasse zur einfacheren Handhabung von MySQL-Befehlen zu schreiben:
wenn ich jetzt aber
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:
MFG - Keks 
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 ..." )
jemand eine Ahnung warum?
Hat sich erledigt.
Keine Ahnung warum es nicht funktioniert hat, aber folgende Loesung funktioniert einwandfrei:
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;