Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

using DBI on Windows2000 with similar code:
for(@JpgList){ dbh = DBI->connect('Blah', 'Blah', 'Blah', 'Blah'); $dbh->{LongReadLen} = 65535; $stmt = $dbh->prepare(" UPDATE Images SET Name = $_ " ); $stmt->execute; $stmt->finish; $dbh->disconnect; rename("$_","Processed/$_"); }
The problem is that I cannot rename, move or unlink the file. When I remove all of the DBI related stuff I can. Someone please to explain this?

Replies are listed 'Best First'.
Re: DBI won't let go!
by dws (Chancellor) on Dec 06, 2002 at 20:22 UTC
    The problem is that I cannot rename, move or unlink the file. When I remove all of the DBI related stuff I can. Someone please to explain this?

    Not with the (lack of) information that you've provided, though I have a suspicion. Add some error checking, and report out the error.   rename($_, "Processed/$_") or die "$_: $!"; Also, add error checking to your DBI calls. And I suspect that your UPDATE query isn't doing what you expect it do.

      I think that dws and I have the same suspicions (I may be wrong). If you are going to the extent of doing a prepare then reuse it. Try :-
      my $dbh = DBI->connect('Blah', 'Blah', 'Blah', 'Blah'); $dbh->{LongReadLen} = 65535; my $stmt = $dbh->prepare(" UPDATE Images SET Name = ?" ); for my $file (@JpgList){ my $rv; $rv = $stmt->execute or die $stmt->errstr; $stmt->execute($file); rename("$_","Processed/$_"); } $stmt->finish; $dbh->disconnect;
      "execute"
      $rv = $sth->execute or die $sth->errstr;
      $rv = $sth->execute(@bind_values) or die $sth->errstr;

      Perform whatever processing is necessary to execute the prepared statement. An "undef" is returned if an error occurs. A successful "execute" always returns true regardless of the number of rows affected, even if it's zero (see below). It is always important to check the return status of "execute" (and most other DBI methods) for errors.

      For a non-"SELECT" statement, "execute" returns the number of rows affected, if known. If no rows were affected, then "execute" returns "0E0", which Perl will treat as 0 but will regard as true. Note that it is not an error for no rows to be affected by a statement. If the number of rows affected is not known, then "execute" returns -1.

      PS do you really want to do an unconstrained update to the images table?

      Hope it helps
      UnderMine

Re: DBI won't let go!
by strider corinth (Friar) on Dec 06, 2002 at 20:46 UTC
    This may be off-topic, but it may be helpful to you anyway. There are a couple of odd things about your code. I don't know the specifics of what you're trying to do, but the reason that the prepare() and execute() statements are separated is so that you don't have to do a connect() each time you want to do something in the database. Your program will run a lot faster like this:
    $dbh = DBI->connect( 'Blah', 'Blah', 'Blah', 'Blah' ); # $dbh->LongReadLen( 65535 ); # since you're not fetching any data, th +is line doesn't do anything useful $stmt = $dbh->prepare( "UPDATE Images SET Name = ?" ); for(@JpgList){ $stmt->execute( $_ ); rename("$_","Processed/$_"); } $stmt->finish; $dbh->disconnect;
    It's possible that changing this will fix the problem. DBI could be blocking on any of those commands. You should probably include some of the error checking mentioned in the DBI documentation, and make sure you're using strict and warnings.
    --
    Love justice; desire mercy.
Re: DBI won't let go!
by mpeppler (Vicar) on Dec 06, 2002 at 22:08 UTC
    To add to what dws and UnderMine have said:

    You are running an update with no where clause. If the table is large-ish this can take a significant amount of time, and is the reason why DBI seems to hang. I'm almost 100% certain that this sort of update is not what you want.

    Michael

Re: DBI won't let go!
by perrin (Chancellor) on Dec 06, 2002 at 20:23 UTC
    What error message are you getting? Maybe $_ is being changed somewhere in there. Try replacing it with a named variable.
Re: DBI won't let go!
by Anonymous Monk on Dec 06, 2002 at 20:27 UTC
    The error is permission denied. ? Earlier in same script I am the one who moves it there, so seems like same script could rename it, right?