สำหรับนักพัฒนาระบบในภาษา
Python จะมีความรู้เล็กๆน้อยๆมาบอกเกี่ยวกับ
MSSQL (SQL Server) สำหรับภาษา python แล้วสามารถเชื่อมต่อกับฐานข้อมูลได้หลายตัวกันที่เดียว แต่จะมาบอกวิธีการเชื่อมต่อ python ร่วมกับ SQL Server กันครับ โดยจะต้องทำการติดตั้งตัว package ของ python ที่มีชื่อว่า
pyodbc กันก่อนนะครับ ขั้นตอนกันจะมีดังนี้
Python ที่ใช้จะเป็น 3.6
ติดตั้ง pyodbc
- Python Knowledge-1.png (28.71 KiB) Viewed 3078 times
จากนั้นทำการ import pyodbc เข้ามาในไฟล์
ทำการเชื่อมต่อโดยจะต้องมี servername กับ databasename ของตัว SQL Server
- Python Knowledge-1.png (30.58 KiB) Viewed 3078 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 (209.22 KiB) Viewed 3078 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/
สำหรับนักพัฒนาระบบในภาษา [url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2417-python-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]Python[/url] จะมีความรู้เล็กๆน้อยๆมาบอกเกี่ยวกับ [url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2266-sql-server-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]MSSQL (SQL Server)[/url] สำหรับภาษา python แล้วสามารถเชื่อมต่อกับฐานข้อมูลได้หลายตัวกันที่เดียว แต่จะมาบอกวิธีการเชื่อมต่อ python ร่วมกับ SQL Server กันครับ โดยจะต้องทำการติดตั้งตัว package ของ python ที่มีชื่อว่า [url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99-php/83-python/2759-pyhton-connect-postgresql.html]pyodbc[/url] กันก่อนนะครับ ขั้นตอนกันจะมีดังนี้
Python ที่ใช้จะเป็น 3.6
ติดตั้ง pyodbc
[code]
pip install pyodbc
[/code]
[attachment=2]Python Knowledge-1.png[/attachment]
จากนั้นทำการ import pyodbc เข้ามาในไฟล์
[code]
import pyodbc
[/code]
ทำการเชื่อมต่อโดยจะต้องมี servername กับ databasename ของตัว SQL Server
[attachment=1]Python Knowledge-1.png[/attachment]
แล้วให้ทำการเขียนคำสั่งตามนี้ จะเป็นคำสั่งเชื่อมต่อฐานข้อมูล
[code]
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)
[/code]
ต่อไปจะเป็นการแสดงข้อมูลที่อยู่ตารางออกมาแสดง คำสั่งตามด้านล่าง ตัวของคำสั่ง SQL ตามตารางในฐานข้อมูลของคุณนะครับจะไม่เหมือนกันขึ้นอยู่กับข้อมูลนะครับ
[code]
cursor = conn.cursor()
cursor.execute('SELECT st.*, emp.studentcode FROM [Transcantime] AS st LEFT JOIN [Student] AS emp ON st.enrollnumber = emp.enrollnumber;')
[/code]
จากนั้นจึงทำการ loop ข้อมูลในตารางออกมาดูโดยใช้คำสั่งนี้
[code]
rows = cursor.fetchall()
for row in rows:
print(row)
[/code]
ผลที่ได้จะได้แบบนี้ครับ
[attachment=0]Python Knowledge-1.png[/attachment]
full code
[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)
[/code]
[url=https://www.youtube.com/watch?v=qBeoKOIPASc&list=PLLi1ciqQAf8TVFbJpl7XY23lzdKrpxsMM]ศึกษา Python พื้นจากจาก Playlist [/url]
[url]https://www.mindphp.com/vdo-tutorial-python.html[/url]
สอน Python พื้นฐานจาก Mindphp.com
[url]https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/83-python.html[/url]
เทคนิค Python
[url]https://www.mindphp.com/developer/tips-python.html[/url]
บทเรียน Python GUI
[url]https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/python-gui.html[/url]
บทเรียน Python Framework Flask
[url]https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/python-framework-flask.html[/url]
บทเรียน Python Tensorflow (เขียน AI)
[url]https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/python-tensorflow.html[/url]
อ้างอิง
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/