โดยทั่วไป developers ใช้ shell scripts เพื่อทำให้ database tasks 自動化 เช่น exporting และ importing data, gathering database (DB) statistics, tablespace monitoring, blocking sessions, DB locks ฯลฯ
ภาพรวม
Python® กลายเป็นหนึ่งในภาษาโปรแกรมมิ่งที่นิยมมากที่สุดในช่วง 7 ปีที่ผ่านมา บทความนี้ครอบคลุม DB modules ต่างๆ ที่ใช้ใน Python สำหรับ database engines ต่างๆ และแสดงวิธีใช้ PyMongo module เพื่อ query และ insert ข้อมูลเข้า MongoDB® collection
Intro to Python Modules
Python เป็น object-oriented และ high-level programming language มันมี modules หลากหลายสำหรับ implementations ต่างๆ เช่น web development, data science, Linux® administration ฯลฯ ตารางต่อไปนี้แสดงรายละเอียดเกี่ยวกับ database engine และ Python modules ที่เกี่ยวข้อง:
| Database Engine | Python Module |
|---|---|
| Oracle | cx_oracle |
| PostgreSQL | psycopg2 |
| MongoDB | pymongo |
| MySQL | mysql.connector และ pymysql |
| Cassandra | cassandra-driver |
CX_ORACLE
cx_oracle เป็น Python module ที่ช่วยให้คุณเข้าถึง Oracle databases และปฏิบัติตาม Python database API specifications Module นี้ compatible กับ Oracle Database 11.2, 12c, 18c, 19c และ 21c
PSYCOPG2
psycopg2 เป็น Python module ที่ช่วยให้คุณเข้าถึง PostgreSQL databases และปฏิบัติตาม Python database API specifications หนึ่งใน key features คือ มันเปิดใช้งาน multi-threaded applications ที่เปิด multiple cursors เพื่อทำ concurrent inserts และ updates
PYMONGO
pymongo เป็น module ที่ช่วยให้คุณเข้าถึง mongo databases และทำ database queries บน MongoDB database objects เช่น collections, documents ฯลฯ
MYSQL.CONNECTOR
mysql.connector เป็น module ที่ช่วยให้คุณเข้าถึง MySQL databases และปฏิบัติตาม Python database API specifications มันเปิดใช้งาน database queries
CASSANDRA-DRIVER
cassandra-driver เป็น Python driver สำหรับ Apache® Cassandra® databases และ compatible กับ Cassandra query language (CQL) โดยใช้ module นี้ คุณสามารถ connect และ query Cassandra databases ได้
PyMongo Demonstration
Python programming ทำ CRUD (Create, Read, Update, และ Delete) operations ต่างๆ ใน MongoDB ขั้นตอนต่อไปนี้แสดงวิธีดาวน์โหลด pip, ติดตั้ง pymongo module และเขียนและ execute Python script ด้วย PyMongo:
-
รันคำสั่งต่อไปนี้เพื่อตรวจสอบ version ของ Python บน Linux server ของคุณ:
# python --version Python 2.7.18 -
ใช้คำสั่งต่อไปนี้เพื่อติดตั้ง pip และ PyMongo:
# yum install python-pip # pip install pymongo -
เปิด text editor ที่คุณชอบ และเขียน Python script ต่อไปนี้ (mongo-crud.py) เพื่อทำ CRUD operations บน collection ใน MongoDB:
#!/usr/bin/python from pymongo import MongoClient client = MongoClient(host = 'localhost:27017', username="dba", password="user123") mongodbinfo = client.server_info() print ("Printing MongoDB Server Information") print (mongodbinfo) print ("##################################################") print ("Mongo DB version") print ("db version:", mongodbinfo["version"]) print ("##################################################") db = client.empdb empdetails = db.employeedetails print ("Existing collection information") printcollection = empdetails.find() for i in printcollection: print (i) print ("##################################################") print ("Adding one document to the collection") details = {"name": "Andy", "designation": "Clerk"} newentry = empdetails.insert_one(details) print (" newentry:", newentry) printcollection1 = empdetails.find() for i in printcollection1: print (i) print ("##################################################") print ("Performing a search on the collection") search = empdetails.find_one({ 'name' : 'Jack'}) print (search) print ("##################################################") print ("Adding multiple documents to the collection") entry1 = {"name": "Cathy", "designation": "Data Operator"} entry2 = {"name": "Susan", "designation": "Operations Manager"} newentries = empdetails.insert_many([entry1, entry2]) printcollection2 = empdetails.find() for i in printcollection2: print (i) -
Execute Python script โดยรันคำสั่งต่อไปนี้ Output จะคล้ายกับภาพต่อไปนี้:
# python mongo-crud.py
สรุป
Python เป็นหนึ่งในภาษาโปรแกรมมิ่งที่ trending มากที่สุดในปี 2020 พร้อม implementations หลากหลาย Python ให้ database engineers ทางเลือกสำหรับ shell scripting ในการทำ database task automation
เรียนรู้เพิ่มเติมเกี่ยวกับบริการฐานข้อมูลของเรา
ใช้แท็บ Feedback เพื่อแสดงความคิดเห็นหรือถามคำถาม คุณยังสามารถเริ่มการสนทนากับเราได้