Saturday 8 August 2020

Perl DB Connection Tutorial with Different databases

 Perl provides support for connecting and interacting with a variety of databases. Here are some examples of connecting to different databases using Perl:

Method 1: Connecting to MySQL using DBI

The Perl DBI (Database Interface) module provides a consistent interface for connecting to and interacting with different databases. Here's an example of connecting to a MySQL database using DBI:


#!/usr/bin/perl

use strict;
use warnings;
use DBI;

my $dsn = "DBI:mysql:database=testdb;host=localhost";
my $user = "username";
my $password = "password";

my $dbh = DBI->connect($dsn, $user, $password) or die "Can't connect to database: $DBI::errstr\n";
# Perform database operations here

$dbh->disconnect();

This code connects to a MySQL database named testdb running on the local machine using the username and password specified. Once the connection is established, you can perform database operations using the $dbh handle. Finally, the disconnect() method is called to close the connection.

Method 2: Connecting to PostgreSQL using DBD::Pg

DBD::Pg is a Perl module that provides support for connecting to and interacting with PostgreSQL databases. Here's an example of connecting to a PostgreSQL database using DBD::Pg:

use strict;
use warnings;
use DBI;

my $dsn = "DBI:Pg:database=testdb;host=localhost";
my $user = "username";
my $password = "password";


my $dbh = DBI->connect($dsn, $user, $password) or die "Can't connect to database: $DBI::errstr\n";
# Perform database operations here
$dbh->disconnect();

This code connects to a PostgreSQL database named testdb running on the local machine using the username and password specified. Once the connection is established, you can perform database operations using the $dbh handle. Finally, the disconnect() method is called to close the connection.

Method 3: Connecting to SQLite using DBD::SQLite

DBD::SQLite is a Perl module that provides support for connecting to and interacting with SQLite databases. Here's an example of connecting to an SQLite database using DBD::SQLite:

#!/usr/bin/perl

use strict;
use warnings;
use DBI;

my $dsn = "DBI:SQLite:test.db";
my $user = "";
my $password = "";


my $dbh = DBI->connect($dsn, $user, $password) or die "Can't connect to database: $DBI::errstr\n";
# Perform database operations here
$dbh->disconnect();

This code connects to an SQLite database file named test.db using an empty username and password. Once the connection is established, you can perform database operations using the $dbh handle. Finally, the disconnect() method is called to close the connection.

Method 4: Connecting to Oracle using DBD::Oracle

DBD::Oracle is a Perl module that provides support for connecting to and interacting with Oracle databases. Here's an example of connecting to an Oracle database using DBD::Oracle:

#!/usr/bin/perl

use strict;
use warnings;
use DBI;

my $dsn = "DBI:Oracle:host=localhost;sid=orcl";
my $user = "username";
my $password = "password";


my $dbh = DBI->connect($dsn, $user, $password) or die "Can't connect to database: $DBI::errstr\n";

# Perform database operations here

$dbh->disconnect();

This code connects to an Oracle database named orcl running on the local machine using the username and password specified. Once the connection is established, you can perform database operations using the $dbh handle. Finally, the disconnect() method is called to close the connection.

Method 5: Connecting to Microsoft SQL Server using DBD::ODBC

DBD::ODBC is a Perl module that provides support for connecting to and interacting with Microsoft SQL Server databases using the ODBC (Open Database Connectivity) API. Here's an example of connecting to a Microsoft SQL Server database using DBD::ODBC:

#!/usr/bin/perl

use strict;
use warnings;
use DBI;

my $dsn = "DBI:ODBC:Driver={SQL Server};Server=localhost;Database=testdb";
my $user = "username";
my $password = "password";


my $dbh = DBI->connect($dsn, $user, $password) or die "Can't connect to database: $DBI::errstr\n";
# Perform database operations here
$dbh->disconnect();

This code connects to a Microsoft SQL Server database named testdb running on the local machine using the username and password specified. Once the connection is established, you can perform database operations using the $dbh handle. Finally, the disconnect() method is called to close the connection.

Method 6: Connecting to MongoDB databases using MongoDB::MongoClient

MongoDB::MongoClient is a Perl module that provides support for connecting to and interacting with MongoDB databases. Here's an example of connecting to a MongoDB database using MongoDB::MongoClient:

#!/usr/bin/perl

use strict;

use warnings;

use MongoDB;

my $client = MongoDB::MongoClient->new(host => 'localhost', port => 27017);

my $db = $client->get_database('testdb');

my $collection = $db->get_collection('testcoll');

# Perform database operations here

$client->disconnect();

This code connects to a MongoDB database named testdb running on the local machine using the default port. Once the connection is established, you can perform database operations using the $collection handle. Finally, the disconnect() method is called to close the connection.

Method 7: Connecting to Redis databases using Redis::Client

Redis::Client is a Perl module that provides support for connecting to and interacting with Redis databases. Here's an example of connecting to a Redis database using Redis::Client:

#!/usr/bin/perl


use strict;
use warnings;
use Redis::Client;


my $client = Redis::Client->new();
$client->connect('localhost', 6379);

# Perform database operations here

$client->disconnect();This code connects to a Redis database running on the local machine using the default port. Once the connection is established, you can perform database operations using the $client handle. Finally, the disconnect() method is called to close the connection.


Method 8: Connecting to Couchbase databases using Couchbase::Client

Couchbase::Client is a Perl module that provides support for connecting to and interacting with Couchbase databases. Here's an example of connecting to a Couchbase database using Couchbase::Client:

#!/usr/bin/perl

use strict;
use warnings;
use Couchbase::Client;


my $client = Couchbase::Client->new(servers => ['http://localhost:8091/pools']);
my $bucket = $client->bucket('default');
# Perform database operations here
$client->disconnect();

This code connects to a Couchbase server running on the local machine using the default port. Once the connection is established, you can perform database operations using the $bucket handle. Finally, the disconnect() method is called to close the connection.

Method 9: Connecting to Cassandra databases using Net::Cassandra::Easy

Net::Cassandra::Easy is a Perl module that provides support for connecting to and interacting with Cassandra databases. Here's an example of connecting to a Cassandra database using Net::Cassandra::Easy:

#!/usr/bin/perl

use strict;

use warnings;

use Net::Cassandra::Easy;

my $client = Net::Cassandra::Easy->new(keyspace => 'mykeyspace', servers => ['localhost:9160']);

$client->connect();

# Perform database operations here

$client->disconnect();

This code connects to a Cassandra database named mykeyspace running on the local machine using the default port. Once the connection is established, you can perform database operations using the $client handle. Finally, the disconnect() method is called to close the connection.

Method 10: Connecting to Microsoft SQL Server databases using DBD::Sybase

DBD::Sybase is a Perl module that provides support for connecting to and interacting with Microsoft SQL Server databases. Here's an example of connecting to a Microsoft SQL Server database using DBD::Sybase:

#!/usr/bin/perl

use strict;

use warnings;

use DBI;

my $dbh = DBI->connect('dbi:Sybase:server=server_name;database=database_name', 'username', 'password');

# Perform database operations here

$dbh->disconnect()

this code connects to a Microsoft SQL Server database running on a server named server_name and using a database named database_name. The username and password for the database are also specified. Once the connection is established, you can perform database operations using the $dbh handle. Finally, the disconnect() method is called to close the connection.


Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home