Perl is a dynamic programming language. It is often used to code the back-end for applications that use a database to store information.
To connect to a database, Perl uses the DBI (Database Independent Interface) module. The DBI module provides a layer of abstraction between our Perl code and the database.
To communicate with the database, DBI uses Structured Query Language otherwise knows as SQL.
To connect to a database, the connect()
method is invoked.
The function takes the following input parameters:
Data source name: This is a string that contains the name of our module, the database engine, and the name of the database that we wish to connect to.
Username: This string represents the username specific to the database we are trying to connect to
Password: The password for the database.
my $db_engine = "mysql";
my $database = "SAMPLEDB";
my $dsn = "DBI:$db_engine:$database";
my $user = "sampleuser";
my $pass = "sample123";
my $dbcon = DBI->connect($dsn, $user, $pass) or die $DBI::errstr();
If the connection is successful, a Database Handle is returned. In case of an error $dbcon
is set to undef
and $DBI::errstr
returns an error string.
To prepare queries, the prepare()
method is invoked. This is invoked via the connection variable.
An SQL query embedded within a string is passed as the input parameter to the prepare()
method.
my $query = $dbcon->prepare("CREATE TABLE Employees(id INT PRIMARY KEY, name VARCHAR(10))");
$query->execute()
The above query is used to create a table in our database.
The execute()
method executes the query in our database. The result table returned (if any) is then associated with our query variable.
The following functions can be used to access the row data of the result table returned.
fetchrow():
returns the next row of the table or undef
in case of error.
fetchrow_array():
returns an array that contains the row data of the current row.
We can disconnect from the database using the
disconnect()
method of the query variable.
The following code shows how a database is connected to and accessed in Perl.
# import the DBI moduleuse DBI;# connectingmy $db_engine = "mysql";my $database = "SAMPLEDB";my $dsn = "DBI:$db_engine:database=$database";my $userid = "sampleuser";my $password = "sample123";my $dbcon = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr();# using queries#--Creating tablemy $query_ = $dbcon->prepare("CREATE TABLE Employees(id INT PRIMARY KEY, name VARCHAR(10)");$query->execute()#--Inserting valuesmy $query = $dbcon->prepare("INSERT INTO Employees VALUES(?, ?)");my $id = 1;my $name = "Alice";$query->execute($id, $name);$query->execute($id + 1, "Bob");#--Selecting all rows in Employees tablemy $resultTable = $dbcon->prepare("SELECT * FROM Employees");$resultTable->execute();#--printing resultwhile (my @row = $resultTable->fetchrow_array()){print "@row\n";}warn "Error", $resultTable->errstr( ), "\n"if $resultTable->err();# disconneting$dbcon->disconnect()exit;
Note: The above code assumes that a database by the name
SAMPLEDB
has been created on the database server.
Free Resources