Hướng dẫn pyodbc python sql server - máy chủ pyodbc python sql

Chuyển đến nội phân

Trình Duyệt nào Không CNn Đan Hỗ trợ nữa.

Hãy nâng cấp lênn microsoft ed

Bước 3: Bằng chứng về khái niệm kết nối với SQL bằng PYODBC

  • Bài viết
  • 19/09/2022
  • 2 Phú

Trong bài viết nào

Ví dụ này là một bằng chứng của khái niệm. Mã mẫu được đơn giản hóa cho rõ ràng và không nhất thiết phải đại diện cho các thực tiễn tốt nhất được đề xuất bởi Microsoft.

Để bắt đầu, hãy chạy tập lệnh mẫu sau. Tạo một tệp có tên Test.py và thêm từng đoạn mã khi bạn đi.

> python test.py

Liên kết

import pyodbc 
# Some other example server values are
# server = 'localhost\sqlexpress' # for a named instance
# server = 'myserver,port' # to specify an alternate port
server = 'tcp:myserver.database.windows.net' 
database = 'mydb' 
username = 'myusername' 
password = 'mypassword' 
# ENCRYPT defaults to yes starting in ODBC Driver 18. It's good to always specify ENCRYPT=yes on the client side to avoid MITM attacks.
cnxn = pyodbc.connect('DRIVER={ODBC Driver 18 for SQL Server};SERVER='+server+';DATABASE='+database+';ENCRYPT=yes;UID='+username+';PWD='+ password)
cursor = cnxn.cursor()

Chạy truy vấn

Hàm con trỏ.execute có thể được sử dụng để truy xuất kết quả được đặt từ truy vấn đối với cơ sở dữ liệu SQL. Hàm này chấp nhận một truy vấn và trả về một tập kết quả, có thể được lặp lại với việc sử dụng con trỏ.fetchone ().

#Sample select query
cursor.execute("SELECT @@version;") 
row = cursor.fetchone() 
while row: 
    print(row[0])
    row = cursor.fetchone()

Chèn một hàng

Trong ví dụ này, bạn thấy cách chạy một câu lệnh chèn một cách an toàn và vượt qua các tham số. Các thông số bảo vệ ứng dụng của bạn khỏi SQL Incer.

#Sample insert query
count = cursor.execute("""
INSERT INTO SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) 
VALUES (?,?,?,?,?)""",
'SQL Server Express New 20', 'SQLEXPRESS New 20', 0, 0, CURRENT_TIMESTAMP).rowcount
cnxn.commit()
print('Rows inserted: ' + str(count))

Azure Active Directory và chuỗi kết nối

PYODBC sử dụng trình điều khiển Microsoft ODBC cho SQL Server. Nếu phiên bản trình điều khiển ODBC của bạn là 17.1 trở lên, bạn có thể sử dụng chế độ tương tác Azure Active Directory của trình điều khiển ODBC thông qua PYODBC.

Tùy chọn tương tác này hoạt động nếu Python và PyoDBC cho phép trình điều khiển ODBC hiển thị hộp thoại. Tùy chọn chỉ có sẵn trên các hệ điều hành Windows.

Example connection string for Azure Active Directory interactive authentication

The following example provides an ODBC connection string that specifies Azure Active Directory interactive authentication:

server=Server;database=Database;UID=UserName;Authentication=ActiveDirectoryInteractive;Encrypt=yes;

For more information about the authentication options of the ODBC driver, see Using Azure Active Directory with the ODBC Driver.

Next steps

For more information, see the Python Developer Center.

Phản hồi

Gửi và xem ý kiến phản hồi dành cho

Chuyển đến nội dung chính

Trình duyệt này không còn được hỗ trợ nữa.

Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.

Step 1: Configure development environment for pyodbc Python development

  • Bài viết
  • 09/14/2022
  • 2 phút để đọc

Trong bài viết này

This article explains how to configure your development environment for pyodbc Python development.

Windows

  1. Download Python installer. If your machine doesn't have Python, install it. Go to the Python download page and download the appropriate installer. For example, if you are on a 64-bit machine, download the Python 3.10 (x64) installer.. If your machine doesn't have Python, install it. Go to the Python download page and download the appropriate installer. For example, if you are on a 64-bit machine, download the Python 3.10 (x64) installer.

  2. Install Python. Once the installer is downloaded, do the following steps:. Once the installer is downloaded, do the following steps:

    1. Double-click the file to start the installer.
    2. Select your language, and agree to the terms.
    3. Follow the instructions on the screen to install Python on your computer.
    4. You can verify that Python is installed by going to a command prompt and running python -V. You can also search for Python in the start menu.
  3. Install the Microsoft ODBC Driver for SQL Server on Windows..

  4. Open cmd.exe as an administrator..

  5. Install pyodbc using pip - Python package manager..

    pip install pyodbc  
    

Linux

Installing on Linux is similar. If the instructions below don't work, see the pyODBC Install instructions, which have more details for different Linux distributions.

  1. Open terminal..

  2. Install Microsoft ODBC Driver for SQL Server on Linux..

  3. Install pyodbc..

    sudo -H pip install pyodbc
    

Next steps

Create an SQL database for pyodbc.

Phản hồi

Gửi và xem ý kiến phản hồi dành cho