Hướng dẫn cursor sql server python

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/14/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' 
cnxn = pyodbc.connect['DRIVER={ODBC Driver 18 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password]
cursor = cnxn.cursor[]

Run query

The cursor.executefunction 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;

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

Lời tựa

pymssql Mô -đun là kết nối với cơ sở dữ liệu sql server [một tiêu chuẩn giao diện toàn cầu cơ sở dữ liệu]. Ngoài ra, pyodbc không giới hạn ở SQL server, nhưng cũng Oracle,MySQL,Access,Excel, v.v.

Ngoài ra, ngoài pymssql, pyodbc, có một số mô -đun kết nối với SQL server. Nếu bạn quan tâm, bạn có thể tìm thấy nó ở đây://wiki.python.org/moin/SQL%20Server

Bài viết này sẽ giới thiệu nội dung có liên quan của Python Thư viện pymssql

Chủ Đề