WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Perl · Advanced · question 46 of 100

Explain how to use Perl’s ’DBI’ module to interact with databases, including connecting, querying, and updating data.?

📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

The Perl DBI (Database Interface) module is a robust database abstraction layer that provides a consistent interface for communicating with a variety of database management systems (DBMS). By using the Perl DBI module, you can connect to a multitude of databases such as MySQL, Oracle, PostgreSQL, SQLite, and more.

To interact with databases using the Perl DBI module, we need to follow the fundamental steps:

1. Import the DBI module by using the ’use’ statement:

use DBI; 

2. Establish a connection to the database by creating a handle using ’connect’ method.

my $dbh = DBI->connect("dbi:mysql:database=your_db;host=your_host", "username", "password", {RaiseError => 1}) 
or die "Couldn't connect to database: " . DBI->errstr;

Here, ’dbi:mysql’ is the driver, followed by the database name ’your_db’ and the host name ’your_host’. We have also passed ’RaiseError=>1’ to the connection attributes to ensure that the DBI module automatically raises exceptions when an error occurs.

3. Once we have established a connection, we can execute SQL queries using the ’prepare’ method. The prepare method returns a statement handle that we can use to execute the query.

my $sth = $dbh->prepare("SELECT * FROM my_table");

We have passed the SQL statement as a parameter to the prepare method. We can also pass variables to the SQL statement using placeholders ’?’.

my $sth = $dbh->prepare("SELECT * FROM my_table WHERE id = ?");
$sth->execute(1);

Here, we have passed the value ’1’ as a parameter to the execute method.

4. Once prepared, we can execute the statement handle using the ’execute’ method.

 $sth->execute(); 

5. We can iterate through the result-set using the ’fetchrow_array’, ’fetchrow_arrayref’ or ’fetchall_arrayref’ methods.

while(my $row = $sth->fetchrow_arrayref) {
    print "@$rown";
}

Here, fetchrow_arrayref returns a reference to an array that contains the row data. We have used the ’@$row’ notation to dereference the array reference and print the row data.

6. We can also use the execute method to perform UPDATE, DELETE, and INSERT operations.

my $sth = $dbh->prepare("INSERT INTO my_table VALUES (?, ?)");
$sth->execute(1, 'John');

Here, we have passed two parameters to the execute method to insert the values ’1’ and ’John’ into the ’my_table’ table.

7. Finally, we must close the statement handle and database handle using ’finish’ and ’disconnect’ methods, respectively.

$sth->finish();
$dbh->disconnect();

In summary, the Perl DBI module provides a simple and consistent interface for interacting with databases. We can use the ’connect’, ’prepare’, ’execute’, and ’fetch’ methods to interact with databases and perform CRUD operations on the data.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Perl interview — then scores it.
📞 Practice Perl — free 15 min
📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

All 100 Perl questions · All topics