Python-TDS is a pure Python DBAPI driver for Microsoft SQL Server, implementing the Tabular Data Stream (TDS) protocol. This cross-platform library eliminates dependencies on ADO or FreeTDS, offering features like MARS, bulk insert, table-valued parameters, and TLS/Kerberos support. The current version is 1.17.1, with active development and consistent releases.
pip install python-tdsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to connect to a Microsoft SQL Server database using `python-tds`, execute a simple query, and fetch results. It uses environment variables for connection details for security and flexibility. The connection and cursor objects are managed using Python's `with` statement for automatic resource cleanup.
Ensure your `python-tds` client is up-to-date. Do not set `TrustServerCertificate` to `true`. Use `HostNameInCertificate` parameter in `pytds.connect` to specify the trusted certificate server name and ensure proper certificate validation.
For binary values, use `pytds.Binary(your_bytes_object)`. Example: `cursor.execute('INSERT INTO MyTable (BinaryColumn) VALUES (%s)', (pytds.Binary(data),))`It is recommended to use `SpnegoAuth` for NTLM/Kerberos authentication instead of the deprecated `pytds.login` approach.
Use `pip install --user python-tds` to install into the user's home directory, or if installing system-wide, use `sudo pip install python-tds` (with caution).
Use `server='your_host'` for default instances or when `dsn` is not required. Use `dsn='your_host\your_instance'` if connecting to a named instance, or `server='your_host', port=your_port` for specific ports.
Install the library using pip: `pip install python-tds`
Verify the username and password used in the connection string and ensure the user has appropriate permissions to connect to the SQL Server and database.
Check the server's IP address/hostname, ensure the SQL Server is running, verify the port (default 1433) is open, and check firewall settings on both the client and server.
Configure `python-tds` to use TLS by setting `encrypt=True` in the connection string, and potentially `trust_server_certificate=True` if using self-signed certificates or for development/testing: `tds.connect(host='...', database='...', user='...', password='...', encrypt=True, trust_server_certificate=True)`.