CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: DBI:
Here, what is the use of finish() ?

 



nagalenoj
Novice

Nov 14, 2008, 6:36 AM

Post #1 of 4 (3619 views)
Here, what is the use of finish() ? Can't Post

Dear friends,

sub read_config {
# connecting to the database
my $dsn = "DBI:Pg:database=nagalenoj";
my $conf_dbh = DBI->connect($dsn, "nagalenoj", "nagalenoj", {RaiseError => 0}) or new_log("$0: Error while databse connection") and exit(1);

# query to execute.
my $statement = $conf_dbh->prepare("select * from dbi.free_conf");

# select the first row from the table, since we can have only one configuration data, it's enough to read only the first row. So, if the table has more than a row, the row won't be considered.

$statement->execute();
$statement->bind_columns(\$options, \$interval, \$keep_records, \$rec_category, \$logfile, \$cont_if_exist);
$statement->fetch(); #revert back.
$statement->finish(); ## commented out for checking.

new_log ("Configurations:options-$options,interval-$interval,keep_record-$keep_records,record_category-$rec_category,logfile-$logfile,cont_if_exist-$cont_if_exist");
#disconnecting the database connection
#$conf_dbh->DESTROY();
$conf_dbh->disconnect;
}

In the above coding, I am reading a configuration table and I assigned in some variables.
The configuration table is having only one row. So, I am fetching that one row and I did the disconnect. But, It shows an error that I have to use the finish() to end the statement.
After adding the call to finish() function, the program is working without errors.

So, Can anyone tell me the use of finish() function.


FishMonger
Veteran / Moderator

Nov 14, 2008, 6:58 AM

Post #2 of 4 (3617 views)
Re: [nagalenoj] Here, what is the use of finish() ? [In reply to] Can't Post

Your select statement will return every row in the database, but the fetch() method only retrieves one of those rows at a time and is normally used within a while loop.

If you only want to retrieve only 1 row, then either use the limit clause in the select statement, or use the finish() method to "tell" the dbh object that you don't want to process the remaining rows that it returned.


nagalenoj
Novice

Nov 14, 2008, 8:53 PM

Post #3 of 4 (3604 views)
Re: [FishMonger] Here, what is the use of finish() ? [In reply to] Can't Post

I have only one row in the table, still why is it necessary to use the finish().


FishMonger
Veteran / Moderator

Nov 15, 2008, 11:03 AM

Post #4 of 4 (3585 views)
Re: [nagalenoj] Here, what is the use of finish() ? [In reply to] Can't Post


In Reply To
I have only one row in the table, still why is it necessary to use the finish().


Your db handle doesn't know that there is only 1 row in the database and until it "does know" via the fetch call returning undef or is "told to stop" via the object's finish() method, it assumes that there are more rows to be fetched.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives