Basic ExampleΒΆ

All interactions with cTDS first require a connection. Connections are created using the ctds.connect() method. All ctds.Connection objects can be used as context managers in a with statement.

In order to actually perform queries or called stored procedures, a ctds.Cursor is required. This can be acquired using the ctds.Connection.cursor() method. Like the ctds.Connection object, the ctds.Cursor object can also be used as a context manager.

Note

Due to the design of the TDS protocol, ctds.Cursor objects cannot have multiple query results active at once. Only the results from the last query are available from the ctds.Cursor.

An example of a simple query follows:

import ctds

connection = ctds.connect(
    'my-database-host',
    user='username',
    password='password'
)
with connection:
    with connection.cursor() as cursor:
        cursor.execute('SELECT @@VERSION AS Version')
        row = cursor.fetchone()

# Columns can be accessed by index, name, or attribute.
assert row[0] == row['Version']
print(row.Version)