Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How PostgreSQL connects to Perl

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "PostgreSQL how to connect Perl", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "PostgreSQL how to connect Perl" bar!

Installation

PostgreSQL can use the Perl DBI module with Perl, which is a database access module integration in the Perl programming language. It defines a set of methods, variables and conventions, and provides a standard database interface.

Here are the simple steps to install the DBI module on the Linux/Unix machine:

$wget http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.625.tar.gz $tar xvfz DBI-1.625.tar.gz $cd DBI-1.625$ perl Makefile.PL $make $make install

If you need to install the driver of DBI's SQLite, then it can be installed as follows:

$wget http://search.cpan.org/CPAN/authors/id/T/TU/TURNSTEP/DBD-Pg-2.19.3.tar.gz $tar xvfz DBD-Pg-2.19.3.tar.gz $cd DBD-Pg-2.19.3$ perl Makefile.PL $make $make install

To start using the interface of Perl's PostgreSQL, you need to find the pg_hba.conf file in the PostgreSQL installation directory and add the following line:

# IPv4 local connections: host all all 127.0.0.1/32 md5

You can use the following command to start / restart Postgres's server if it is not running:

[root@host] # service postgresql restart Stopping postgresql service: [OK] Starting postgresql service: [OK]

DBI interface APIs

The following are important DBI routines that you can use to manipulate the SQLite database using the Perl program as required. For more complex applications, you can take a look at the official Perl DBI documentation.

Connect to the database

The following Perl code shows how to connect to an existing database. If the database does not exist, it will be created and a database object will finally be returned.

#! / usr/bin/perl

Use DBI

Use strict

My $driver = "Pg"

My $database = "testdb"

My $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432"

My $userid = "postgres"

My $password = "pass123"

My $dbh = DBI- > connect ($dsn, $userid, $password, {RaiseError = > 1})

Or die $DBI::errstr

Print "Opened database successfully\ n"

Now let's run the above program to open our database testdb. If we successfully open the database, it will send the following message:

Open database successfully

Create a tabl

The following Perl program creates a table in the previously created database:

#! / usr/bin/perl

Use DBI

Use strict

My $driver = "Pg"

My $database = "testdb"

My $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432"

My $userid = "postgres"

My $password = "pass123"

My $dbh = DBI- > connect ($dsn, $userid, $password, {RaiseError = > 1})

Or die $DBI::errstr

Print "Opened database successfully\ n"

My $stmt = qq (CREATE TABLE COMPANY

(ID INT PRIMARY KEY NOT NULL

NAME TEXT NOT NULL

AGE INT NOT NULL

ADDRESS CHAR (50)

SALARY REAL);)

My $rv = $dbh- > do ($stmt)

If ($rv

< 0){ print $DBI::errstr; } else { print "Table created successfully\n"; } $dbh->

Disconnect ()

When the above program executes, it creates a COMPANY table company in the database testdb, and it displays the following message:

Opened database successfully Table created successfully

INSERT operation

The Perl program, which shows how we created records in the COMPANY table in the above example:

#! / usr/bin/perl use DBI; use strict; my $driver = "Pg"; my $database = "testdb"; my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432"; my $userid = "postgres"; my $password = "pass123"; my $dbh = DBI- > connect ($dsn, $userid, $password, {RaiseError = > 1}) or die $DBI::errstr; print "Opened database successfully\ n" My $stmt = qq (INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Paul', 32,' California', 20000.00)); my $rv = $dbh- > do ($stmt) or die $DBI::errstr; $stmt = qq (INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Allen', 25,' Texas', 15000.00)); $rv = $dbh- > do ($stmt) or die $DBI::errstr $stmt = qq (INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'Teddy', 23,' Norway', 20000.00)); $rv = $dbh- > do ($stmt) or die $DBI::errstr; $stmt = qq (INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Mark', 25,' Rich-Mond', 65000.00); $rv = $dbh- > do ($stmt) or die $DBI::errstr; print "Records created successfully\ n" $dbh- > disconnect ()

When the above program executes, it creates a record in the COMPANY table and displays the following two lines:

Opened database successfully Records created successfully

SELECT operation

The Perl program, which shows how we can get and show the records in the table COMPANY created in the above example:

#! / usr/bin/perl use DBI; use strict; my $driver = "Pg"; my $database = "testdb"; my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432"; my $userid = "postgres"; my $password = "pass123"; my $dbh = DBI- > connect ($dsn, $userid, $password, {RaiseError = > 1}) or die $DBI::errstr; print "Opened database successfully\ n"; my $stmt = qq (SELECT id, name, address, salary from COMPANY;) My $sth = $dbh- > prepare ($stmt); my $rv = $sth- > execute () or die $DBI::errstr; if ($rv)

< 0){ print $DBI::errstr; } while(my @row = $sth->

Fetchrow_array () {print "ID =". $row [0]. "\ n"; print "NAME =" $row [1]. "\ n"; print "ADDRESS =". $row [2]. "\ n"; print "SALARY =". $row [3]. "\ n\ n";} print "Operation done successfully\ n"; $dbh- > disconnect ()

When the above program is executed, it produces the following results:

Opened database successfully ID = 1 NAME = Paul ADDRESS = California SALARY = 20000 ID = 2 NAME = Allen ADDRESS = Texas SALARY = 15000 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000 Operation done successfully

UPDATE operation

The Perl code shows how we can update any record using the update statement, and then get the updated record from the COMPANY table and display it:

#! / usr/bin/perl use DBI; use strict; my $driver = "Pg"; my $database = "testdb"; my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432"; my $userid = "postgres"; my $password = "pass123"; my $dbh = DBI- > connect ($dsn, $userid, $password, {RaiseError = > 1}) or die $DBI::errstr; print "Opened database successfully\ n"; my $stmt = qq (UPDATE COMPANY set SALARY = 25000.00 where ID=1;) My $rv = $dbh- > do ($stmt) or die $DBI::errstr; if ($rv

< 0 ){ print $DBI::errstr; }else{ print "Total number of rows updated : $rv\n"; } $stmt = qq(SELECT id, name, address, salary from COMPANY;); my $sth = $dbh->

Prepare ($stmt); $rv = $sth- > execute () or die $DBI::errstr; if ($rv

< 0){ print $DBI::errstr; } while(my @row = $sth->

Fetchrow_array () {print "ID =". $row [0]. "\ n"; print "NAME =" $row [1]. "\ n"; print "ADDRESS =". $row [2]. "\ n"; print "SALARY =". $row [3]. "\ n\ n";} print "Operation done successfully\ n"; $dbh- > disconnect ()

When the above program is executed, it produces the following results:

Opened database successfully Total number of rows updated: 1 ID = 1 NAME = Paul ADDRESS = California SALARY = 25000 ID = 2 NAME = Allen ADDRESS = Texas SALARY = 15000 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000 Operation done successfully

DELETE operation

The Perl code shows how we use the delete statement to delete any records, then get the COMPANY table and display the remaining records:

#! / usr/bin/perl use DBI; use strict; my $driver = "Pg"; my $database = "testdb"; my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432"; my $userid = "postgres"; my $password = "pass123"; my $dbh = DBI- > connect ($dsn, $userid, $password, {RaiseError = > 1}) or die $DBI::errstr; print "Opened database successfully\ n"; my $stmt = qq (DELETE from COMPANY where ID=2;) My $rv = $dbh- > do ($stmt) or die $DBI::errstr; if ($rv

< 0 ){ print $DBI::errstr; }else{ print "Total number of rows deleted : $rv\n"; } $stmt = qq(SELECT id, name, address, salary from COMPANY;); my $sth = $dbh->

Prepare ($stmt); $rv = $sth- > execute () or die $DBI::errstr; if ($rv

< 0){ print $DBI::errstr; } while(my @row = $sth->

Fetchrow_array () {print "ID =". $row [0]. "\ n"; print "NAME =" $row [1]. "\ n"; print "ADDRESS =". $row [2]. "\ n"; print "SALARY =". $row [3]. "\ n\ n";} print "Operation done successfully\ n"; $dbh- > disconnect ()

When the above program is executed, it produces the following results:

Opened database successfully Total number of rows deleted: 1 ID = 1 NAME = Paul ADDRESS = California SALARY = 25000 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000 Operation done successfully Thank you for your reading, the above is the content of "how PostgreSQL connects Perl". After the study of this article, I believe you have a deeper understanding of how PostgreSQL connects Perl, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report