Devdit
 

Python MongoDB เชื่อมต่อฐานข้อมูล

660

Python MongoDB เชื่อมต่อฐานข้อมูล บทความนี้สอนเขียนโปรแกรมภาษา Python เชื่อมต่อฐานข้อมูล MongoDB ด้วย module pymongo โดยใช้คำสั่ง MongoClient พร้อมแสดงผลลัพธ์การเชื่อมต่อฐานข้อมูลออกสู่หน้าจอ สามารถเขียนโปรแกรมได้ดังนี้

 

ตัวอย่าง Python MongoDB เชื่อมต่อฐานข้อมูล

from pymongo import MongoClient

conn = "mongodb://localhost:27017/example"
client = MongoClient(conn)
db = client.get_database()
print( db )

ผลลัพธ์

Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'example')

คำอธิบาย

Python MongoDB เชื่อมต่อฐานข้อมูล จากตัวอย่างเขียนโค้ดภาษา Python เชื่อมต่อฐานข้อมูล MogoDB ชื่อ example จาก Connection String mongodb://localhost:27017 โดยใช้คำสั่ง MongoClient พร้อมแสดงผลลัพธ์การเชื่อมต่อฐานข้อมูล MongoDB ด้วย get_database ออกสู่หน้าจอร่วมกับคำสั่ง print

แก้ไข 1 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ