Connection
- class k_ctds.Connection
A connection to the database server.
- __enter__()
Enter the connection’s runtime context. On exit, the connection is closed automatically.
- Returns:
The connection object.
- Return type:
- __exit__(exc_type, exc_val, exc_tb)
Exit the connection’s runtime context, closing the connection. If no error occurred, any pending transaction will be committed prior to closing the connection. If an error occurred, the transaction will be implicitly rolled back when the connection is closed.
- Parameters:
- Returns:
- __repr__()
Return repr(self).
- autocommit
Auto-commit transactions after
k_ctds.Cursor.execute(),k_ctds.Cursor.executemany(), andk_ctds.Cursor.callproc(). IfFalse, operations must be committed explicitly usingcommit().- Return type:
- bulk_insert(table, rows, batch_size=None, tablock=False, auto_encode=False)
Bulk insert rows into a given table. This method utilizes the BULK INSERT functionality of SQL Server to efficiently insert large amounts of data into a table. By default, rows are not validated until all rows have been processed.
An optional batch size may be specified to validate the inserted rows after batch_size rows have been copied to server.
- Parameters:
table (str) – The table in which to insert the rows.
rows (typeiter) – An iterable of data rows. Data rows are Python sequence objects. Each item in the data row is inserted into the table in sequential order. Version 1.9 supports passing rows as
dict. Keys must map to column names and must exist for all non-NULL columns.batch_size (int) – An optional batch size.
tablock (bool) – Should the TABLOCK hint be passed?
auto_encode (bool) – Should Python str values be automatically encoded based on the target column’s collation? When True, column metadata is queried from INFORMATION_SCHEMA.COLUMNS before the insert begins. str values destined for NVARCHAR/NCHAR/NTEXT columns are encoded to UTF-16LE. str values destined for VARCHAR/CHAR/TEXT columns are encoded to the column’s collation code page. .. versionadded:: 2.0.0
- Returns:
The number of rows saved to the table.
- Return type:
- close()
Close the connection now. Pending transactions will be rolled back. Subsequent calls to this object or any
k_ctds.Cursorobjects it created will raisek_ctds.InterfaceError.
- commit()
Commit any pending transaction to the database.
- cursor()
Return a new
k_ctds.Cursorobject using the connection.Note
k_ctds.Cursor.close()should be called when the returned cursor is no longer required.Warning
Only one
k_ctds.Cursorobject should be used per connection. The last command executed on any cursor associated with a connection will overwrite any previous results from all other cursors.- Returns:
A new Cursor object.
- Return type:
- messages
A list of any informational messages received from the last
k_ctds.Cursor.execute(),k_ctds.Cursor.executemany(), ork_ctds.Cursor.callproc()call. For example, this will include messages produced by the T-SQL PRINT and RAISERROR statements. Messages are preserved until the next call to any of the above methods.Noneis returned if the connection is closed.Added in version 1.4.
- rollback()
Rollback any pending transaction to the database.
- spid
The SQL Server Session Process ID (SPID) for the connection or
Noneif the connection is closed.- Return type:
- tds_version
The TDS version in use for the connection or
Noneif the connection is closed.- Return type:
- timeout
The connection timeout, in seconds, or
Noneif the connection is closed.Note
Setting the timeout requires FreeTDS version 1.00 or later.
- Raises:
k_ctds.NotSupportedError – cTDS was compiled against a version of FreeTDS which does not support setting the timeout on a connection.
- Return type:
String Representation
repr(connection) returns a human-readable summary useful for
debugging:
>>> conn = k_ctds.connect('localhost', user='sa', password='secret')
>>> repr(conn)
"<k_ctds.Connection database='master' spid=54>"
>>> conn.close()
>>> repr(conn)
'<k_ctds.Connection (closed)>'
Added in version 2.1.0.