สองทาง คือ ทั้งฝั่ง Server และ Client นอกจากกนี้ยังใช้เป็นตัวกลางในการติดต่อ ประมวลผล
ระหว่าง Sever กับ Server เช่น Web server กับ Database server เป็นต้น ในบทความนี้ผู้เขียนจะ
กล่าวถึงการเขียนโปรแกรมภาษา Perl ให้ติดต่อกับ PostgreSQL Sever และสั่งให้ประมวลผล
ง่าย ๆ
ก่อนอื่นต้องติดตั้งภาษา Perl และ PostgreSQL Server ก่อน เมื่อต้องการติดต่อ Post
submarine@Debian77:~$
submarine@Debian77:~$ su
Password:
root@Debian77:/home/submarine#
root@Debian77:/home/submarine# apt-get install libdbd-pg-perl
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
libdbd-pg-perl
0 upgraded, 1 newly installed, 0 to remove and 85 not upgraded.
Need to get 217 kB of archives.
After this operation, 539 kB of additional disk space will be used.
Get:1 http://ftp.us.debian.org/debian/ wheezy/main libdbd-pg-perl i386 2.19.2-2 [217 kB]
Fetched 217 kB in 2s (99.2 kB/s)
Selecting previously unselected package libdbd-pg-perl.
(Reading database ... 153988 files and directories currently installed.)
Unpacking libdbd-pg-perl (from .../libdbd-pg-perl_2.19.2-2_i386.deb) ...
Processing triggers for man-db ...
Setting up libdbd-pg-perl (2.19.2-2) ...
root@Debian77:/home/submarine# exit
exit
submarine@Debian77:~$
submarine@Debian77:~$ ls
C Documents images Music Perl Public Videos
Desktop Downloads Java openSUSE-13.2-GNOME-Live-x86_64.iso Pictures Templates www
submarine@Debian77:~$ cd Perl/
submarine@Debian77:~/Perl$ pico connect_postgresql.pl
submarine@Debian77:~/Perl$ chmod +x connect_postgresql.pl
submarine@Debian77:~/Perl$ ./connect_postgresql.pl
2+2=4
submarine@Debian77:~/Perl$
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "web";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432";
my $userid = "submarine";
my $password = "xxxxxxxxxxxx";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfully...เย้!...ติดต่อฐานข้อมูลได้แล้ว\n";
submarine@Debian77:~/Perl$ ./connect_postgresql1.pl
Opened database successfully...เย้!...ติดต่อฐานข้อมูลได้แล้ว
submarine@Debian77:~/Perl$
ไม่มีความคิดเห็น:
แสดงความคิดเห็น