วันจันทร์ที่ 12 มกราคม พ.ศ. 2558

การเขียนโปรแกรมภาษา Perl ติดต่อ PostgreSQL Database Server ผ่าน Web Sever Apache

        หลังจากเราปรับแต่ง ทดลองเขียนโปรแกรมภาษา Perl ให้แสดงผลบน Web server และเขียน
โปรแกรมภาษา Perl ให้ติดต่อ PostgreSQL Database server โดยใช้ DBI ได้แล้ว  ในบทความนี้เราจะ
นำทั้งสองเรื่องมารวมในไฟล์เดียวกัน เพื่อนให้ Web server สามารถใช้ภาษา Perl ไปติดต่อกับ
PostgreSQL database server แล้วนำข้อมูลมาแสดงผลบน Web server โดยใช้ Browser ได้
        วิธีการ คือ นำคำสั่ง  print "Content-type: text/plain\n\n" ; มาใส่ไว้บนหัวของไฟล์ .pl ที่เก็บอยู่ใน
ไดเรกทอรีที่สามารถใช้งานภาษา Perl ได้บน Web server ไนที่นี้เราจะเก็บไฟล์ภาษา perl ไว้ที่
ไดเรกทอรี่ /usr/lib/cgi-bin
        ตัวอย่าง ไฟล์ connect_postgresql.pl

#!/usr/bin/perl

print "Content-type: text/plain\n\n" ;
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";

เขียนไฟล์เสร็จแล้วกำหนดให้อ่าน เขียนไฟล์ได้ด้วยทำสั่ง chmod +x connect_postgresql1.pl
แล้วเรียกใช้งานตาม url ที่กำหนดไว้  ในที่นี้ผู้เขียนจะเรียกใช้งานไฟล์ดังกล่าวที่
http://localhost/cgi-bin-connect_postgresql1.pl  ได้ผลดังภาพ



        เมื่อสามารถเปิดฐานข้อมูลโดยใช้ภาษา perl เชื่อมต่อฐานข้อมูล PostgreSQL โดยให้นำมา
แสดงผลบน Web server apache ที่อ่านด้วยบราวเซอร์ได้แล้ว ต่อไปจะเป็นตัวอย่างการสร้างตาราง
บนฐานข้อมูล  ผู้เขียนสร้างฐานข้อมูล ชื่อ debian ด้วยแพคเกจ phpPgAdmin ที่เป็นโปรแกรม
จัดการฐานข้อมูล PostgreSQL ด้วยภาษา Php

       ตัวอย่างไฟล์  create_table.pl   เพื่อสร้างตารางชื่อ persons บนฐานข้อมูล ชื่อ debian


#!/usr/bin/perl

print "Content-type: text/plain\n\n" ;

use DBI;
use strict;

my $driver   = "Pg"; 
my $database = "debian";
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";

my $stmt = qq(CREATE TABLE persons
      (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();


        กำหนดสิทธิ์ไฟล์ให้อ่าน เขียนได้ด้วยคำสั่ง chmod +x create_table.pl แล้วรันโปรแกรมด้วย
คำสั่ง ./create_table.pl  หรือจะเรียกใช้งานผ่านบราวเซอร์ก็ได้


        เมื่อมีฐานข้อมูล และตารางแล้ว ไฟล์ต่อไปนี้เป็นตัวอย่างไฟล์ภาษา Perl ที่เขียนขึ้นเพื่อ
ใส่ข้อมูล (insert) ในตารางที่สร้างขึ้น  ในที่นี้จะตั้งชื่อไฟล์ insert_data.pl


#!/usr/bin/perl
print "Content-type: text/plain\n\n" ;

use DBI;
use strict;

my $driver   = "Pg"; 
my $database = "debian";
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";

my $stmt = qq(INSERT INTO persons (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (1, 'นพ.วีระพงษ์  ศรีชาติ', 51, 'ฉะเชิงเทรา', 100000.00 ));
my $rv = $dbh->do($stmt) or die $DBI::errstr;

$stmt = qq(INSERT INTO persons (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (2, 'นพ.ประสิทธิ์  ศรีชาติ', 46, 'ปราจีณบุรี', 150000.00 ));
$rv = $dbh->do($stmt) or die $DBI::errstr;

$stmt = qq(INSERT INTO persons (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (3, 'นายสมจิตร  ศรีชาติ', 43, 'สกลนคร', 40000.00 ));
$rv = $dbh->do($stmt) or die $DBI::errstr;

$stmt = qq(INSERT INTO persons (ID,NAME,AGE,ADDRESS,SALARY)
      VALUES (4, 'นายผเด็จ  ศรีชาติ', 37, 'สกลนคร', 25000.00 ););
$rv = $dbh->do($stmt) or die $DBI::errstr;

print "Records created successfully\n";
$dbh->disconnect();

        หลังเขียนโปรแกรมเสร็จก็คำเช่นเดียวกัน คือ กำหนดสิทธิ์ของไฟล์ และรันโปรแกรม  ถ้า
ไม่มีข้อผิดพลาดก็ศึกษาไฟล์ตัวอย่างต่อไปเพื่อนำข้อมูลในตารางบนฐานข้อมูลมาแสดงทั้งบน
Command line และแสดงผลบนบราวเซอร์
     
        ต่อไปนี้เป็นตัวอย่างไฟล์ select_data.pl ให้นำข้อมูลในตารางมาแสดงผล 


#!/usr/bin/perl

print "Content-type: text/plain\n\n" ;

use DBI;
use strict;

my $driver   = "Pg"; 
my $database = "debian";
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";

my $stmt = qq(SELECT id, name, address, salary  from persons;);
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();

        หลังกำหนดสิทธิ์ สั่งรัน และแสดงผลด้วยบราวเซอร์ได้ผลดังภาพข้างล่างนี้



        สิ่งที่ยังต้องพัฒนาต่อ คือ การเขียนโปรแกรมให้รับข้อมูลไปใส่ในตาราง และปรับการ
แสดงผลผ่านบราวเซอร์ให้นำเสนอข้อมูลเป็นตารางเพื่อความสวยงามซึ่งทำได้ไม่ยากนัก
เพียงทบทวนการเขียนตารางบนภาษา HTML อีกเล้กน้อยก็ทำได้แล้ว
       จากบทความนี้จะเห็นว่าเราสามารถใช้งาน Debian 7.7 GNU/Linux, Apache, PostgreSQL,
Perl และ Perl DBI มาใช้งานเพื่อจัดการฐานข้อมูลได้อย่างมีประสิทธิภาพ


ไม่มีความคิดเห็น:

แสดงความคิดเห็น