Easy Manual Installation and Basic Usage of PurePerl MySQL API
Using the PurePerl MySQL API – Part 1
Writing a Perl Module
Foreword: In this part of the series, I explain how easy it is to use and install the PurePerl MySQL API; I also explain how to connect, create and select a database.
By: Chrysanthus Date Published: 28 Jan 2015
Introduction
In this series, I show you how to use the PurePerl MySQL API by creating and using a database. The activities are similar to what you will find in the MySQL manual. You should also consider the activities as a test of ease of use and robustness of the API; and have confidence in the API.
Hey, the API is free, but do not forget to read the terms of agreement.
If you are not using the Windows Operating System, then precede any code sample in this series with something like, #!/usr/bin/perl . The code samples in this series use the console (Prompt or DOS Window) for output.
In this series, you are assumed to be the root user of the MySQL database server.
Pre-Knowledge
This series is part of my volume, Writing a Perl Module. However, to be able to understand this series, you should have professional knowledge in Perl, professional knowledge in MySQL, and database in general. If you do not have that knowledge, click the relevant links below (bottom of page) to have the knowledge first.
In each series you should read the parts in the order given. For each part, the links and order are in a menu on the (top) left of the page.
The API
The API consists of Perl functions, which you use to access the MySQL server. The functions come in a module. To use the module, all you need to do is to learn how to use the functions (and how to install the module) – you will be surprised to see how easy it is to install and use the module. The module with other packages form a library.
The link to download the software is given below. The file to download is a zipped directory. You download it and you unzipp it. After unzipping, you will see the file, Mysql.pm and the directory, Mysql.
Installation is easy: copy the file, Mysql.pm and the directory, Mysql to the directory, C:/Perl/lib or similar of your computer. That is all! Begin to use the API (functions and variables) if you already know how to use it; if you do not already know how to use the API, then continue learning to the end of this series; after which you can start using it.
Note: if you are not using the Windows Operating System, then go to the file Mysql.pm of your computer, in the installed directory, and at the top of the file, type something like, #!/usr/bin/perl pushing down the rest of the content, by one line. Do the same for all the other files in the C:/Perl/lib/Mysql directory of your computer. Begin work!
PurePerl MySQL API
Basic Usage of the API
The syntax of the function to connect to the server is:
Boolean Mysql::connect("username", "password", "domain_name", port, [database_name])
As you can see the database name is optional.
All the variables and functions are defined in the file, Mysql.pm . So, to access a variable of the API, in your script (application), you have to begin with “$Mysql” without the quotes and then the double colon, “::”. One of the variables in the API is, Error_msg. So to access it you will type:
$Mysql::Error_msg
The connect function returns 1 on success and 0 on failure.
If connection fails, to obtain the error message (string), use the expression:
$Mysql::Error_msg
To access any function in your script, you begin with “Mysql” without the quotes and then the double colon operator (::) and then the name of the function in the API. An example is the connect() function syntax above. The main file of the API is Msql.pm .
Selecting A Database
Whether or not you selected a database in the connect function, you can still select a new database with the function, whose syntax is:
Boolean Mysql::select_db("database_name")
The function returns 1 on success and 0 on failure. If the function fails, to obtain the error message, use the expression:
$Mysql::Error_msg
Status Report
You can tell the server to send back a string containing a brief status report. The syntax of the function to do this is:
Boolean Mysql::stats()
The function returns 1 on success and 0 on failure. The function does not return the report string. To obtain the report string, use the expression,
$Mysql::Message
where $Message is a variable in the API.
If the function fails, to get the error message, use the expression,
$Mysql::Error_msg
To know whether the server is alive and reachable, use the ping function. The syntax is:
Boolean Mysql::ping()
The function returns 1 on success and 0 on failure. The return of 1 shows that the server is alive and reachable.
Basic Code Sample
The following code gives you the basics on how to code your script. It assumes you already have a MySQL account. It also assumes you are using localhost and the default port, 3306 . All examples in this series assume you are using localhost as the domain and the default MySQL port of 3306. The code below uses all the above functions. You will have to type in your own username and password.
use Mysql;
use strict;
#connect
if (Mysql::connect("user", "xxxxxx", "localhost", 3306) != 1)
{
print $Mysql::Error_msg;
}
else
{
#issue your queries!
#select a new database to work with
my $db = "test";
if (Mysql::select_db($db) != 1)
{
print $Mysql::Error_msg, "\n";
}
else
{
#start and continue to work with the database
}
#Obtain brief status report
if (Mysql::stats() != 1)
{
print $Mysql::Error_msg, "\n";
}
else
{
print $Mysql::Message, "\n";
}
#ping the server
if (Mysql::ping() != 1)
{
print $Mysql::Error_msg, "\n";
}
else
{
print "Server is alive and reachable", "\n";
}
}
I tried it in my computer, there was no problem, and I had the following feedback:
Uptime: 5795 Threads: 1 Questions: 2 Slow queries: 0 Opens: 33 Flush tables
: 1 Open tables: 0 Queries per second avg: 0.000
Server is alive and reachable
Now, note that at the top of the code above, you have,
use Mysql;
The statement ends with a semicolon. This is an instruction to the computer to use the functions and variables of the Mysql.pm file (API). The statement does not have “.pm” even though the file has.
Note: your Perl script has the extension, “.pl”, while the files for the packages have the extension, “.pm”.
That is it for this part of the series. I hope you can see how easy it is to install and use the API. Rendezvous in the next part of the series.
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
NEXT