การเขียน Python เชื่อมต่อฐานข้อมูล MSSQL (SQL Server)

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
jirawoot
PHP VIP Members
PHP VIP Members
โพสต์: 3129
ลงทะเบียนเมื่อ: 17/06/2019 10:30 am

การเขียน Python เชื่อมต่อฐานข้อมูล MSSQL (SQL Server)

โพสต์ที่ยังไม่ได้อ่าน โดย jirawoot »

สำหรับนักพัฒนาระบบในภาษา Python จะมีความรู้เล็กๆน้อยๆมาบอกเกี่ยวกับ MSSQL (SQL Server) สำหรับภาษา python แล้วสามารถเชื่อมต่อกับฐานข้อมูลได้หลายตัวกันที่เดียว แต่จะมาบอกวิธีการเชื่อมต่อ python ร่วมกับ SQL Server กันครับ โดยจะต้องทำการติดตั้งตัว package ของ python ที่มีชื่อว่า pyodbc กันก่อนนะครับ ขั้นตอนกันจะมีดังนี้

Python ที่ใช้จะเป็น 3.6

ติดตั้ง pyodbc

โค้ด: เลือกทั้งหมด

pip install pyodbc
Python Knowledge-1.png
Python Knowledge-1.png (28.71 KiB) Viewed 3040 times
จากนั้นทำการ import pyodbc เข้ามาในไฟล์

โค้ด: เลือกทั้งหมด

import pyodbc
ทำการเชื่อมต่อโดยจะต้องมี servername กับ databasename ของตัว SQL Server
Python Knowledge-1.png
Python Knowledge-1.png (30.58 KiB) Viewed 3040 times
แล้วให้ทำการเขียนคำสั่งตามนี้ จะเป็นคำสั่งเชื่อมต่อฐานข้อมูล

โค้ด: เลือกทั้งหมด

SQLSERVER = 'COM011\SQLEXPRESS'
SQLDB = 'test_hip01'
str_conn = 'Driver={SQL Server};Server=%s;Database=%s;Trusted_Connection=yes;'%(SQLSERVER, SQLDB)
conn = pyodbc.connect(str_conn)
ต่อไปจะเป็นการแสดงข้อมูลที่อยู่ตารางออกมาแสดง คำสั่งตามด้านล่าง ตัวของคำสั่ง SQL ตามตารางในฐานข้อมูลของคุณนะครับจะไม่เหมือนกันขึ้นอยู่กับข้อมูลนะครับ

โค้ด: เลือกทั้งหมด

cursor = conn.cursor()
cursor.execute('SELECT st.*, emp.studentcode FROM [Transcantime] AS st LEFT JOIN [Student] AS emp ON st.enrollnumber = emp.enrollnumber;')
จากนั้นจึงทำการ loop ข้อมูลในตารางออกมาดูโดยใช้คำสั่งนี้

โค้ด: เลือกทั้งหมด

rows = cursor.fetchall()
for row in rows:
    print(row)
ผลที่ได้จะได้แบบนี้ครับ
Python Knowledge-1.png
Python Knowledge-1.png (209.22 KiB) Viewed 3040 times
full code

โค้ด: เลือกทั้งหมด

import pyodbc
SQLSERVER = 'COM011\SQLEXPRESS'
SQLDB = 'test_hip01'
str_conn = 'Driver={SQL Server};Server=%s;Database=%s;Trusted_Connection=yes;'%(SQLSERVER, SQLDB)
conn = pyodbc.connect(str_conn)
cursor = conn.cursor()
cursor.execute('SELECT st.*, emp.studentcode FROM [Transcantime] AS st LEFT JOIN [Student] AS emp ON st.enrollnumber = emp.enrollnumber;')
rows = cursor.fetchall()
for row in rows:
    print(row)

ศึกษา Python พื้นจากจาก Playlist

https://www.mindphp.com/vdo-tutorial-python.html
สอน Python พื้นฐานจาก Mindphp.com
https://www.mindphp.com/%E0%B8%9A%E0%B8 ... ython.html
เทคนิค Python
https://www.mindphp.com/developer/tips-python.html
บทเรียน Python GUI
https://www.mindphp.com/%E0%B8%9A%E0%B8 ... n-gui.html
บทเรียน Python Framework Flask
https://www.mindphp.com/%E0%B8%9A%E0%B8 ... flask.html
บทเรียน Python Tensorflow (เขียน AI)
https://www.mindphp.com/%E0%B8%9A%E0%B8 ... rflow.html

อ้างอิง
https://docs.microsoft.com/th-th/sql/connect/python/pyodbc/step-3-proof-of-concept-connecting-to-sql-using-pyodbc?view=sql-server-2017
https://stackoverflow.com/questions/33725862/connecting-to-microsoft-sql-server-using-python
https://www.tutorialgateway.org/connect-python-and-sql-server/
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 89