Skip to content
프로젝트

소개

Python DBAPI driver for MSSQL using pure Python TDS (Tabular Data Stream) protocol implementation

기타 프로젝트

이 수치를 측정하는 방법

What you can do

  • Fully supports new MSSQL 2008 date types: datetime2, date, time, datetimeoffset
  • Table-valued parameters
  • TLS connection encryption
  • Kerberos support on non-Windows platforms (requires kerberos package)

측정된 스타 증가

in 24h
0
in 7 days
+1
in 15 days
+1
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

pytds

https://secure.travis-ci.org/denisenkom/pytds.png?branch=master https://ci.appveyor.com/api/projects/status/a5h4y29063crqtet?svg=true

Python DBAPI driver for MSSQL using pure Python TDS (Tabular Data Stream) protocol implementation. Doesn't depend on ADO or FreeTDS. Can be used on any platform, including Linux, MacOS, Windows.

It can be used with https://pypi.python.org/pypi/django-sqlserver as a Django database backend.

Features

  • Fully supports new MSSQL 2008 date types: datetime2, date, time, datetimeoffset
  • MARS
  • Bulk insert
  • Table-valued parameters
  • TLS connection encryption
  • Kerberos support on non-Windows platforms (requires kerberos package)
  • Token-based authentication

Installation

To install run this command:

$ pip install python-tds

If you want to use TLS you should also install pyOpenSSL package:

$ pip install pyOpenSSL

For a better performance install bitarray package too:

$ pip install bitarray

To use Kerberos on non-Windows platforms (experimental) install kerberos package:

$ pip install kerberos

Documentation

Documentation is available at https://python-tds.readthedocs.io/en/latest/.

Example

To connect to database do

import pytds
with pytds.connect('server', 'database', 'user', 'password') as conn:
    with conn.cursor() as cur:
        cur.execute("select 1")
        cur.fetchall()

or with token-based authentication

import os
import pytds
import msal

SQL_SERVER = 'your-server'
DATABASE = 'your-db'
CAFILE = '/etc/ssl/cert.pem'

def get_access_token():
    tenant_id = os.getenv("TENANT_ID")
    client_id = os.getenv("CLIENT_ID")
    client_secret = os.getenv("CLIENT_SECRET")

    authority = f'https://login.microsoftonline.com/{tenant_id}'
    scope = ['https://database.windows.net/.default']

    app = msal.ConfidentialClientApplication(
        client_id=client_id,
        client_credential=client_secret,
        authority=authority
    )

    result = app.acquire_token_for_client(scopes=scope)
    if "access_token" not in result:
        raise RuntimeError(f"Token acquisition failed: {result.get('error_description')}")
    return result["access_token"]

# Use the token callable when connecting
with pytds.connect(
    server=SQL_SERVER,
    database=DATABASE,
    access_token_callable=get_access_token,
    cafile=CAFILE,
) as conn:
    with conn.cursor() as cur:
        cur.execute('SELECT TOP 1 name FROM sys.databases')
        print(cur.fetchone())

To enable TLS you should also provide cafile parameter which should be a file name containing trusted CAs in PEM format.

For detailed documentation of connection parameters see: pytds.connect

관련 프로젝트 살펴보기

불러오는 중…