Locking a Table and Ending a Session with PurePerl MySQL API
Using the PurePerl MySQL API – Part 7
Writing a Perl Module
Foreword: In this part of the series, I talk about Locking a Table and Ending a Session with PurePerl MySQL
By: Chrysanthus Date Published: 28 Jan 2015
Introduction
Locking Tables
The following code will lock the table, pet for writing.
my $loc = "LOCK TABLES pet WRITE";
if (Mysql::query($loc) != 1)
{
print $Mysql::Error_msg, "\n";
}
else
{
print $Mysql::Message, "\n";
}
Again just place the correct SQL statement without the ending semicolon as argument to the query() function.
After locking you should unlock. The following code unlock all tables locked:
my $loc = "UNLOCK TABLES ";
if (Mysql::query($loc) != 1)
{
print $Mysql::Error_msg, "\n";
}
else
{
print $Mysql::Message, "\n";
}
Ending a Session
The connect statement opens a socket. Any socket opened has to be closed. At the end of your session, you have to close the socket. However, you are not to do the low-level coding; you just use the Mysql package close() function, like:
Mysql::close();
The function returns 1 (true) on success. Any error message on failure is sent to the MySQL package variable, $Error_msg. The close() function also closes the connection. The following code (in your script) closes the socket and the connection at the end of a session:
if (Mysql::close() != 1)
{
print $Mysql::Error_msg;
}
else
{
print "Connection closed.";
}
If you have the privilege to shut down the MySQL server, you can do so. This command should be issued before closing the connection. The shutdown() function shuts down the server (remote) and not the socket. The function is defined in the Mysql package, but in your script, you type something like,
Mysql::shutdown();
The following code segment (in your script) should shut down the server:
if (Mysql::shutdown() != 1)
{
print $Mysql::Error_msg, "\n";
}
else
{
print $Mysql::Message, "\n";
}
End of Article and End of Series
We have come to the end of the article and the end of the series. I hope you appreciated the series. You can now go on to learn web development or another use of the API (see below).
Chrys
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course
BACK NEXT