Skip to content
Jahnvi Thakkar edited this page Jul 2, 2026 · 5 revisions

The Row object in the mssql-python module represents a single row of data retrieved from a SQL query. It is designed to be tuple-like, allowing you to access data using both column names and indices. This object is returned by the cursor's fetch functions, such as fetchone, fetchall, and fetchmany.

Importing the Row Class

The Row class is exported at the top level of mssql_python and can be imported directly for use in type annotations:

frommssql_pythonimportconnect, Rowdefprocess_user(row: Row) ->str:
returnf"{row.name} ({row.email})"conn=connect(conn_str)
cursor=conn.cursor()
cursor.execute("SELECT name, email FROM users")
forrowincursor:
print(process_user(row))

Key Features

Access by Column Name: Retrieve data using the column names.
Access by Index: Retrieve data using the column indices.
Iterate Over Columns: Easily iterate over the columns in a row.

Usage

Creating a Row Object The Row object is created by the cursor when fetching data from a query. You do not need to create Row objects manually.

frommssql_pythonimportconnect# Establish a connectionconn=connect("Server=ServerAddress;Database=myDataBase;UID=myUsername;PWD=myPassword;")
# Create a cursor objectcursor=conn.cursor()
# Execute a SQL querycursor.execute("SELECT * FROM Employees")
# Fetch one rowrow=cursor.fetchone()

Accessing Data

You can access the data in a Row object using either the column names or the column indices.

By Column Name
Note: This will not work if the column name is an invalid Python label (e.g. contains a space or is a Python reserved word)

# Access data by column nameemployee_id=row.EmployeeIDemployee_name=row.EmployeeName

By Index

# Access data by indexemployee_id=row[0]
employee_name=row[1]

Iterating Over Columns
You can iterate over the columns in a Row object to access all the data.

# Iterate over columnsforcolumninrow:
print(column)

Clone this wiki locally