Latest Post

🚨 BẢO MẬT WEB THỜI ĐẠI AI: HACKER DÙNG AI, BẠN VẪN BẢO VỆ WEBSITE THEO CÁCH CŨ? 🚨 CẢNH BÁO BẢO MẬT: HACKER CÓ THỂ ĐANG NHẮM ĐẾN FILE config.php CHỨA MẬT KHẨU DATABASE! UEFI Package sử dụng thư viện xử lý hình ảnh có rất nhiều lỗ hỏng bảo mật. Wireless LAN Router của ELECOM có lỗ hỏng bảo mật OS Command injection Note: setting days for Crontab Launching AWS EC2 Instance using Python Tự động phát hành SSL Certificate hết hạn Let’s Encrypt Verify if crontab works Cách xem thời hạn của Let’s Encrypt SSL Certificate AWS EC2 Instance: jum update failure: repodata/repomd.xml from rpmforge: [Errno 256] No more mirrors to try. Nhập môn Python: Chương 2 Hello World thử viết chương trình python đơn giản. Python – Anaconda: Cài đặt Anaconda môi trường lập trình python Nhập môn Python: Chương 1 Python là gì? MySQL – GRANT: Tạo user, set lại password, xem và cấp quyền cho user. Python – Multithread – Queue – File: Xử lý đa luồng dùng chung dữ liệu trên cùng một file dùng hàng đợi(Queue) ⑤Ubuntu – Postfix – Dovecot : Mail server setting, một phát ăn ngay . ④Ubuntu – Postfix – Dovecot : SSL/TSL security mail server setting, một phát ăn ngay. ③Ubuntu – Postfix – Dovecot : Mail server setting, một phát ăn ngay. ②Ubuntu – Postfix – Dovecot : Mail server setting, một phát ăn ngay. ① Ubuntu – Postfix – Dovecot : Mail server setting, một phát ăn ngay. Python – Pandas: lấy data từ file csv. Newbie – Python – Selenium: Tự động login Facebook với trình duyệt Chrome Newbie – Python – Skype: Gửi Message tới Skype chat group hoặc user bằng ngôn ngữ Python. Newbie – Python – PostgreSQL: Kết nối với cơ sở dữ liêu PostgreSQL bằng ngôn ngữ Python. Newbie – Python – SQL Server: Kết nối với cơ sở dữ liệu SQL Server bằng ngôn ngữ Python. Newbie – Python – Mysql: Kết nối với cơ sở dữ liêu MySQL bằng ngôn ngữ Python. Basic Authentication Setting How to setup AWS VPN Client 【GCP】Compute Engine’s Disk Expansion Linux Commands WordPress 404 Page Error (Post)
0 0
Read Time:2 Minute, 31 Second

Mục lục

Xác định hệ điều hành.

[root@ip-175-34-0-155 ~]# cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"
You have new mail in /var/spool/mail/root

 

Install Driver dùng để kết nối với SQL Server

Để kết nối với cơ sở dữ liêu SQL Server bằng python , ta dùng pyodbc.

Để install pyodbc ta thực thi câu lênh bên dưới.

Hệ điều hành Mac ,Windows ta đều có thể install bằng command.

pip install pyodbc

Hệ điều hành linux ta install bằng command như thứ tự bên dưới.

 yum install unixODBC unixODBC-devel
yum install gcc-c++
sudo yum install python3-devel
pip install pyodbc

Nếu version của Python là 3.x thì dùng câu lệnh dưới đây.

pip3 install pyodbc

 

Cách viết code Python để nối kết với cơ sở dữ liệu SQL Server.

Để kết nối với cơ sở dữ liệu SQL Server ta viết code như dưới đây.

import pyodbc

# Tạo kết nối với cơ sở dữ liệu SQL Server
 instance = "172.0.0.1"
 user = "admin"
 pasword = "mypass"
 db = "test_sql_server"
 #WINDOWS
 connection = "DRIVER={SQL Server};SERVER=" + instance + ";uid=" + user + \
                 ";pwd=" + pasword + ";DATABASE=" + db
 #LINUX
 connection = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=" + instance + ";uid=" + user + \
                 ";pwd=" + pasword + ";DATABASE=" + db

 conn = pyodbc.connect(connection)

# Đóng kết nối với cơ sở dữ liệu SQL Server
 conn.close
  • user:user id
  • passwd:password
  •  instance :Chỉ định host hoặc là IP của server SQL Server
  • db:tên của cơ sở dữ liệu SQL Server cần kết nối .

Thử thực thi một câu lệnh SQL đơn giản.

Thử viết một đoạn code Python đơn giản để thao tác trên cơ sở dữ liệu SQL Server.

Ở đoạn code này ta sẽ kết nối với cơ sỡ dữ liệu SQL Server và thực thi câu lênh select để lấy data từ hai cột host và user của bảng user.

Source Code

import pyodbc

# Tạo kết nối với SQL Server
 instance = "172.0.0.1" 
 user = "admin" 
 pasword = "mypass" 
 db = "test_sql_server" 
 connection = "DRIVER={SQL Server};SERVER=" + instance + ";uid=" + user + \ 
             ";pwd=" + pasword + "; DATABASE=" + db 
 conn = pyodbc.connect(connection)

# Tạo con trỏ để trỏ vào Cơ sở dữ liệu.
 cur = conn.cursor()

# Thực thi câu lệnh SQL
# Lấy Host và User từ bảng user
 sql = "select Host, User from user"
 cur.execute(sql)

# Lấy kết quả khi thực thi câu lệnh sql
 rows = cur.fetchall()

# Hiển thị từng dòng dữ liệu.
 for row in rows:
  print(row)

 cur.close
 conn.close

 

Dưới đây là kết quả khi chạy đoạn code trên.

('127.0.0.1', 'root')
('::1', 'root')
('localhost', 'pma')
('localhost', 'root')

 

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *