Hướng dẫn python library sql server

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 3: Proof of concept connecting to SQL using pyodbc

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

Trong bài viết này

This example is a proof of concept. The sample code is simplified for clarity, and doesn't necessarily represent best practices recommended by Microsoft.

To get started, run the following sample script. Create a file called test.py, and add each code snippet as you go.

> python test.py

Connect

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()

Run query

The cursor.execute function can be used to retrieve a result set from a query against SQL Database. This function accepts a query and returns a result set, which can be iterated over with the use of cursor.fetchone().

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

Insert a row

In this example, you see how to run an INSERT statement safely, and pass parameters. The parameters protect your application from SQL injection.

#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 and the connection string

pyODBC uses the Microsoft ODBC driver for SQL Server. If your version of the ODBC driver is 17.1 or later, you can use the Azure Active Directory interactive mode of the ODBC driver through pyODBC.

This interactive option works if Python and pyODBC permit the ODBC driver to display the dialog. The option is only available on Windows operating systems.

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.

Tải xuống Microsoft Edge Xem thêm thông tin về Internet Explorer và Microsoft Edge

Đọc bằng tiếng Anh

Đọc bằng tiếng Anh Phản hồi Chỉnh sửa

Python SQL driver

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

Trong bài viết này

Install SQL driver for Python

You can connect to a SQL Database using Python on Windows, Linux, or macOS.

Getting started

There are several python SQL drivers available. However, Microsoft places its testing efforts and its confidence in pyodbc driver. Choose one of the following drivers, and configure your development environment:

  • Python SQL driver - pyodbc
  • Python SQL driver - pymssql

Documentation

For documentation, see Python documentation at Python.org.

Community

  • Azure Python Developer Center
  • python.org Community

Next steps

Explore samples that use Python to connect to a SQL database in the following articles:

  • Create a Python app in Azure App Service on Linux
  • Getting Started with Python on Windows
  • Getting Started with Python on macOS
  • Getting Started with Python on Ubuntu
  • Getting Started with Python on Red Hat Enterprise Linux (RHEL)
  • Getting Started with Python on SUSE Linux Enterprise Server (SLES)

Phản hồi

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

Sản phẩm này Trang này

Xem tất cả ý kiến phản hồi về trang

Trong bài viết này