Basic Example

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

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

Note

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

An example of a simple query follows:

import k_ctds

connection = k_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)