การใช้งานภาษา Python มาบ้างแล้วในบทความ เรื่อง การประมวลผลภาพ (Image Processing) ด้วย
ภาษา Python ในบทความนี้จะกล่าวถึงการใช้ภาษา Python เป็นตัวเชื่อม (interface) กับ PostgreSQL
Database server
(Library) python-psycopg2 เพิ่มเติม
submarine@Debian77:~$ su
Password:
root@Debian77:/home/submarine# aptitude install python-psycopg2
The following NEW packages will be installed:
python-egenix-mxdatetime{a} python-egenix-mxtools{a} python-psycopg2
0 packages upgraded, 3 newly installed, 0 to remove and 85 not upgraded.
Need to get 469 kB of archives. After unpacking 1,783 kB will be used.
Do you want to continue? [Y/n/?] y
Get: 1 http://ftp.us.debian.org/debian/ wheezy/main python-egenix-mxtools i386 3.2.1-1.1 [108 kB]
Get: 2 http://ftp.us.debian.org/debian/ wheezy/main python-egenix-mxdatetime i386 3.2.1-1.1 [111 kB]
Get: 3 http://ftp.us.debian.org/debian/ wheezy/main python-psycopg2 i386 2.4.5-1 [250 kB]
Fetched 469 kB in 3s (134 kB/s)
Selecting previously unselected package python-egenix-mxtools.
(Reading database ... 154014 files and directories currently installed.)
Unpacking python-egenix-mxtools (from .../python-egenix-mxtools_3.2.1-1.1_i386.deb) ...
Selecting previously unselected package python-egenix-mxdatetime.
Unpacking python-egenix-mxdatetime (from .../python-egenix-mxdatetime_3.2.1-1.1_i386.deb) ...
Selecting previously unselected package python-psycopg2.
Unpacking python-psycopg2 (from .../python-psycopg2_2.4.5-1_i386.deb) ...
Setting up python-egenix-mxtools (3.2.1-1.1) ...
Setting up python-egenix-mxdatetime (3.2.1-1.1) ...
Setting up python-psycopg2 (2.4.5-1) ...
root@Debian77:/home/submarine# exit
exit
submarine@Debian77:~$ clear
สร้าง Python directory เพื่อเก็บข้อมูล และไฟล์ที่เกียวข้อง
submarine@Debian77:~$ mkdir Python
submarine@Debian77:~$ cd Python/
ลองเรียกใช้โปรแกรมภาษา Python
submarine@Debian77:~/Python$ python
Python 2.7.3 (default, Mar 14 2014, 11:57:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
ออกจากโปรแกรมด้วยคำสั่ง Ctrl+D
ผู้เขียนสร้างฐานข้อมูลบน PostgreSQLชื่อ python
เขียนโปรแกรมภาษา Python ชื่อ connect_database.py เพื่อติดต่อฐานข้อมูล ดังนี้
submarine@Debian77:~/Python$ pico connect_database.py
#!/usr/bin/python
import psycopg2
conn = psycopg2.connect(database="python", user="submarine", password="xxxxxxxxxxxx", host="127.0.0.1", port="543$
print "Opened database successfully"
สั่งรันโปรแกรมด้วยคำสั่ง python และผลการรันโปรแกรม
Opened database successfully
เมื่อโปรแกรมติดต่อฐานข้อมูลได้แล้ว ลองศึกษาตัวอย่างไฟล์ create_table.py เขียนขึ้น
เพื่อสร้างตาราง ชื่อ persons บนฐานข้อมูล python ดังนี้
#!/usr/bin/python
import psycopg2
conn = psycopg2.connect(database="python", user="submarine", password="xxxxxxxxxxxx", host="127.0.0.1", port="5$
print "Opened database successfully"
cur = conn.cursor()
cur.execute('''CREATE TABLE persons
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);''')
print "Table created successfully"
conn.commit()
conn.close()
สั่งรันโปรแกรม และผลลัพธ์
submarine@Debian77:~/Python$ python create_table.py
Opened database successfully
Table created successfully
submarine@Debian77:~/Python$
ตัวอย่างต่อไปนี้เป็นการโปรแกรม ชื่อ ไฟล์ insert_data.py เพื่อใส่ข้อมูลลงในตาราง ชื่อ
persons บนฐานข้อมูลที่ python ที่สร้างขึ้น รายละเอียดโปรแกรมมีดังนี้
#!/usr/bin/python
import psycopg2
conn = psycopg2.connect(database="python", user="submarine", password="xxxxxxxxxxxx", host="127.0.0.1", port="5432")
print "Opened database successfully"
cur = conn.cursor()
cur.execute("INSERT INTO persons (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (1, 'Weerapong Srichaj', 32, 'Chacheongsao', 200000.00 )");
cur.execute("INSERT INTO persons (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (2, 'Prasit Srichaj', 25, 'Prachin Buri', 15000.00 )");
cur.execute("INSERT INTO persons (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (3, 'Somchit Srichaj', 23, 'Skon Nakhon', 20000.00 )");
cur.execute("INSERT INTO persons (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (4, 'Anongnat Srichaj', 25, 'Skon Nakhon ', 65000.00 )");
conn.commit()
print "Records created successfully";
conn.close()
สั่งรันโปรแกรม และผลลัพธ์ปรากฏดังข้อมูลข้างล่างนี้
submarine@Debian77:~/Python$ python insert_data.py
Opened database successfully
Records created successfully
เขียนโปรแกรมชื่อไฟล์ select_data.py เพื่อให้แสดงผลข้อมูลที่ใส่ไว้ในตาราง รายละเอียด
โปรแกรม มีดังนี้
submarine@Debian77:~/Python$ pico select_data.py
#!/usr/bin/python
import psycopg2
conn = psycopg2.connect(database="python", user="submarine", password="xxxxxxxxxxxx", host="127.0.0.1", port="5432")
print "Opened database successfully"
cur = conn.cursor()
cur.execute("SELECT id, name, address, salary from persons")
rows = cur.fetchall()
for row in rows:
print "ID = ", row[0]
print "NAME = ", row[1]
print "ADDRESS = ", row[2]
print "SALARY = ", row[3], "\n"
print "Operation done successfully";
conn.close()
สั่งรันโปรแกรมจะได้ผลลัพธ์ดังรายละเอียดข้างล่างนี้
submarine@Debian77:~/Python$ python select_data.py
Opened database successfully
ID = 1
NAME = Weerapong Srichaj
ADDRESS = Chacheongsao
SALARY = 200000.0
ID = 2
NAME = Prasit Srichaj
ADDRESS = Prachin Buri
SALARY = 15000.0
ID = 3
NAME = Somchit Srichaj
ADDRESS = Skon Nakhon
SALARY = 20000.0
ID = 4
NAME = Anongnat Srichaj
ADDRESS = Skon Nakhon
SALARY = 65000.0
Operation done successfully
submarine@Debian77:~/Python$
จะเห็นว่าภาษา Python เขียนง่าย ใช้งานง่ายไม่ต้องคอมไพล์เหมือนกับภาษา Java, C/C++
จึงเหมาะอย่างยิ่งสำหรับการเขียนโปรแกรมทั้งการแสดงผลผ่าน command line และ web server
ในบทความต่อไปผู้เขียนจะกล่าวถึงการเขียนโปรแกรมภาษา Python ให้ทำงานร่วมกับ Web
server และ PostgreSQL
submarine@Debian77:~/Python$ python create_table.py
Opened database successfully
Table created successfully
submarine@Debian77:~/Python$
ตัวอย่างต่อไปนี้เป็นการโปรแกรม ชื่อ ไฟล์ insert_data.py เพื่อใส่ข้อมูลลงในตาราง ชื่อ
persons บนฐานข้อมูลที่ python ที่สร้างขึ้น รายละเอียดโปรแกรมมีดังนี้
#!/usr/bin/python
import psycopg2
conn = psycopg2.connect(database="python", user="submarine", password="xxxxxxxxxxxx", host="127.0.0.1", port="5432")
print "Opened database successfully"
cur = conn.cursor()
cur.execute("INSERT INTO persons (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (1, 'Weerapong Srichaj', 32, 'Chacheongsao', 200000.00 )");
cur.execute("INSERT INTO persons (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (2, 'Prasit Srichaj', 25, 'Prachin Buri', 15000.00 )");
cur.execute("INSERT INTO persons (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (3, 'Somchit Srichaj', 23, 'Skon Nakhon', 20000.00 )");
cur.execute("INSERT INTO persons (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (4, 'Anongnat Srichaj', 25, 'Skon Nakhon ', 65000.00 )");
conn.commit()
print "Records created successfully";
conn.close()
สั่งรันโปรแกรม และผลลัพธ์ปรากฏดังข้อมูลข้างล่างนี้
submarine@Debian77:~/Python$ python insert_data.py
Opened database successfully
Records created successfully
เขียนโปรแกรมชื่อไฟล์ select_data.py เพื่อให้แสดงผลข้อมูลที่ใส่ไว้ในตาราง รายละเอียด
โปรแกรม มีดังนี้
submarine@Debian77:~/Python$ pico select_data.py
#!/usr/bin/python
import psycopg2
conn = psycopg2.connect(database="python", user="submarine", password="xxxxxxxxxxxx", host="127.0.0.1", port="5432")
print "Opened database successfully"
cur = conn.cursor()
cur.execute("SELECT id, name, address, salary from persons")
rows = cur.fetchall()
for row in rows:
print "ID = ", row[0]
print "NAME = ", row[1]
print "ADDRESS = ", row[2]
print "SALARY = ", row[3], "\n"
print "Operation done successfully";
conn.close()
สั่งรันโปรแกรมจะได้ผลลัพธ์ดังรายละเอียดข้างล่างนี้
submarine@Debian77:~/Python$ python select_data.py
Opened database successfully
ID = 1
NAME = Weerapong Srichaj
ADDRESS = Chacheongsao
SALARY = 200000.0
ID = 2
NAME = Prasit Srichaj
ADDRESS = Prachin Buri
SALARY = 15000.0
ID = 3
NAME = Somchit Srichaj
ADDRESS = Skon Nakhon
SALARY = 20000.0
ID = 4
NAME = Anongnat Srichaj
ADDRESS = Skon Nakhon
SALARY = 65000.0
Operation done successfully
submarine@Debian77:~/Python$
จะเห็นว่าภาษา Python เขียนง่าย ใช้งานง่ายไม่ต้องคอมไพล์เหมือนกับภาษา Java, C/C++
จึงเหมาะอย่างยิ่งสำหรับการเขียนโปรแกรมทั้งการแสดงผลผ่าน command line และ web server
ในบทความต่อไปผู้เขียนจะกล่าวถึงการเขียนโปรแกรมภาษา Python ให้ทำงานร่วมกับ Web
server และ PostgreSQL


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