1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
============================
``pymssql`` module reference
============================
.. module:: pymssql
Complete documentation of ``pymssql`` module classes, methods and properties.
Module-level symbols
====================
.. data:: __version__
pymssql version as an Unicode constant. E.g. ``u"2.1.1"``, ``u"2.2.0"``
.. data:: VERSION
pymssql version in tuple form which is more easily handled (parse, compare)
programmatically. E.g. ``(2, 1, 1)``, ``(2, 2, 0)``
.. versionadded:: 2.2.0
.. data:: __full_version__
pymssql version as an Unicode constant but including any (:pep:`440`)
suffixes. E.g. ``u"2.1.0.dev2"``, ``u"2.2.0.dev"``
Constants, required by the DB-API 2.0 specification:
.. data:: apilevel
``'2.0'`` -- ``pymssql`` strives for compliance with DB-API 2.0.
.. data:: paramstyle
``'pyformat'`` -- ``pymssql`` uses extended python format codes.
.. data:: threadsafety
``1`` -- Module may be shared, but not connections.
Functions
=========
.. function:: connect(server='.', user=None, password=None, database='', \
timeout=0, login_timeout=60, charset='UTF-8', \
as_dict=False, host='', appname=None, port='1433',\
conn_properties=None, autocommit=False, tds_version=None)
Constructor for creating a connection to the database. Returns a
:class:`Connection` object. Note that in most cases you will want to
use keyword arguments, instead of positional arguments.
:param str server: database host
:param str user: database user to connect as
:param str password: user's password
:param str database: The database to initialize the connection with. By
default *SQL Server* selects the database which is set as
default for specific user
:param int timeout: query timeout in seconds, default ``0`` (no timeout)
:param int login_timeout: timeout for connection and login in seconds, default ``60``
:param str charset: character set with which to connect to the database
:param bool as_dict: Whether rows should be returned as dictionaries instead
of tuples. You can access columns by 0-based index or
by name. Please see :doc:`examples </pymssql_examples>`
:param str host: Database host and instance you want to connect to. Valid
examples are:
* ``r'.\SQLEXPRESS'`` -- SQLEXPRESS instance on local machine (Windows only)
* ``r'(local)\SQLEXPRESS'`` -- same as above (Windows only)
* ``'SQLHOST'`` -- default instance at default port (Windows only)
* ``'SQLHOST'`` -- specific instance at specific port set up in freetds.conf (Linux/\*nix only)
* ``'SQLHOST,1433'`` -- specified TCP port at specified host
* ``'SQLHOST:1433'`` -- the same as above
* ``'SQLHOST,5000'`` -- if you have set up an instance to listen on port 5000
* ``'SQLHOST:5000'`` -- the same as above
``'.'`` (the local host) is assumed if host is not provided.
:keyword str appname: Set the application name to use for the connection
:keyword str port: the TCP port to use to connect to the server
:keyword conn_properties: SQL queries to send to the server upon connection
establishment. Can be a string or another kind of
iterable of strings. Default value: See
:class:`_mssql.connect() <_mssql.MSSQLConnection>`
:keyword bool autocommit: Whether to use default autocommiting mode or not
:keyword str tds_version: TDS protocol version to use
.. warning::
Currently, setting *timeout* or *login_timeout* has a process-wide
effect because the FreeTDS db-lib API functions used to implement such
timeouts have a global effect.
.. note::
If you need to connect to Azure read the relevant :doc:`topic </azure>`.
.. versionadded:: 2.1.1
The ability to connect to Azure.
.. versionadded:: 2.1.1
The *conn_properties* parameter.
.. versionadded:: 2.1.1
The *autocommit* parameter.
.. versionadded:: 2.1.2
The *tds_version* parameter.
.. versionchanged:: 2.2.0
The default value of the *tds_version* parameter was changed to ``None``.
In version 2.1.2 its default value was ``'7.1'``.
.. warning::
The *tds_version* parameter has a default value of ``None``. This means two
things:
#. You can't rely anymore in the old ``'7.1'`` default value and
#. Now you'll need to either
* Specify its value explicitly by passing a value for this parameter or
* Configure it using facilities provided by FreeTDS (see `here
<http://www.freetds.org/userguide/freetdsconf.htm#TAB.FREETDS.CONF>`_
and `here <http://www.freetds.org/userguide/envvar.htm>`_)
This might look cumbersome but at the same time means you can now fully
configure the characteristics of a connection to SQL Server from Python
code when using pymssql without using a stanza for the server in the
``freetds.conf`` file or even with no ``freetds.conf`` at all. Up to
version 2.1.1 it simply wasn't possible to control the TDS protocol
version, and in version 2.1.2 it was possible to set it but version 7.1 was
used if not specified.
.. warning::
FreeTDS added sopport for TDS protocol version 7.3 in version 0.95. You
need to be careful of not asking for TDS 7.3 if you know the undelying
FreeTDS used by pymssql is version 0.91 as it won't raise any error nor
keep you from passing such an invalid value.
.. warning::
FreeTDS added support for TDS protocol version 7.3 in version 0.95. You
need to be careful of not asking for TDS 7.3 if you know the undelying
FreeTDS used by pymssql is older as it won't raise any error nor keep you
from passing such an invalid value.
.. function:: get_dbversion()
Wrapper around DB-Library's ``dbversion()`` function which returns the
version of FreeTDS (actually the version of DB-Lib) in string form. E.g.
``"freetds v0.95"``.
Unfortunately 1) The value returned doesn't indicate minor revisions (e.g.
``v0.95.50``), 2) Its data type makes it harder to compare versions or
handle it programmatically in other ways and 3) It hasn't been consistently
updated through the FreeTDS release history.
A pymssql extension to the DB-API 2.0.
.. function:: set_max_connections(number)
Sets maximum number of simultaneous database connections allowed to be open
at any given time. Default is 25.
A pymssql extension to the DB-API 2.0.
.. function:: get_max_connections()
Gets current maximum number of simultaneous database connections allowed to
be open at any given time.
A pymssql extension to the DB-API 2.0.
.. function:: set_wait_callback(wait_callback_callable)
.. versionadded:: 2.1.0
Allows pymssql to be used along cooperative multi-tasking systems and have
it call a callback when it's waiting for a response from the server.
The passed callback callable should receive one argument: The file
descriptor/handle of the network socket connected to the server, so its
signature must be::
def wait_callback_callable(read_fileno):
#...
pass
Its body should invoke the appropiate API of the multi-tasking framework you
are using use that results in the current greenlet yielding the CPU to its
siblings whilst there isn't incoming data in the socket.
See the :doc:`pymssql examples document </pymssql_examples>` for a more
concrete example.
A pymssql extension to the DB-API 2.0.
``Connection`` class
====================
.. class:: Connection(user, password, host, database, timeout, \
login_timeout, charset, as_dict)
This class represents an MS SQL database connection. You can create an
instance of this class by calling constructor :func:`pymssql.connect()`.
Connection object properties
----------------------------
This class has no useful properties and data members.
Connection object methods
-------------------------
.. method:: Connection.autocommit(status)
Where *status* is a boolean value. This method turns autocommit mode on or
off.
By default, autocommit mode is off, what means every transaction must
be explicitly committed if changed data is to be persisted in the database.
You can turn autocommit mode on, what means every single operation commits
itself as soon as it succeeds.
A pymssql extension to the DB-API 2.0.
.. method:: Connection.close()
Close the connection.
.. method:: Connection.cursor()
Return a cursor object, that can be used to make queries and fetch results
from the database.
.. method:: Connection.commit()
Commit current transaction. You must call this method to persist your data if
you leave autocommit at its default value, which is ``False``.
See also :doc:`pymssql examples </pymssql_examples>`.
.. method:: Connection.rollback()
Roll back current transaction.
``Cursor`` class
================
.. class:: Cursor
This class represents a Cursor (in terms of Python DB-API specs) that is used to
make queries against the database and obtaining results. You create
``Cursor`` instances by calling :py:meth:`~Connection.cursor()` method on
an open :py:class:`Connection` connection object.
Cusor object properties
-----------------------
.. attribute:: Cursor.rowcount
Returns number of rows affected by last operation. In case of ``SELECT``
statements it returns meaningful information only after all rows have been
fetched.
.. attribute:: Cursor.connection
This is the extension of the DB-API specification. Returns a reference to the
connection object on which the cursor was created.
.. attribute:: Cursor.lastrowid
This is the extension of the DB-API specification. Returns identity value of
last inserted row. If previous operation did not involve inserting a row into
a table with identity column, ``None`` is returned.
.. attribute:: Cursor.rownumber
This is the extension of the DB-API specification. Returns current 0-based
index of the cursor in the result set.
Cusor object methods
--------------------
.. method:: Cursor.close()
Close the cursor. The cursor is unusable from this point.
.. method:: Cursor.execute(operation)
Cursor.execute(operation, params)
*operation* is a string and *params*, if specified, is a simple value, a
tuple, a dict, or ``None``.
Performs the operation against the database, possibly replacing parameter
placeholders with provided values. This should be preferred method of
creating SQL commands, instead of concatenating strings manually, what makes
a potential of `SQL Injection attacks`_. This method accepts formatting similar
to Python's builtin :ref:`string interpolation operator
<python:string-formatting>`. However, since formatting and type conversion is handled
internally, only the ``%s`` and ``%d`` placeholders are supported. Both placeholders are
functionally equivalent.
Keyed placeholders are supported if you provide a dict for *params*.
If you call ``execute()`` with one argument, the ``%`` sign loses its
special meaning, so you can use it as usual in your query string, for
example in ``LIKE`` operator. See the :doc:`examples </pymssql_examples>`.
You must call :meth:`Connection.commit()` after ``execute()`` or your data
will not be persisted in the database. You can also set
``connection.autocommit`` if you want it to be done automatically. This
behaviour is required by DB-API, if you don't like it, just use the
:mod:`_mssql` module instead.
.. method:: Cursor.executemany(operation, params_seq)
*operation* is a string and *params_seq* is a sequence of tuples (e.g. a
list). Execute a database operation repeatedly for each element in parameter
sequence.
.. method:: Cursor.fetchone()
Fetch the next row of a query result, returning a tuple, or a dictionary if
as_dict was passed to ``pymssql.connect()``, or ``None`` if no more data is
available. Raises ``OperationalError`` (:pep:`249#operationalerror`) if
previous call to ``execute*()`` did not produce any result set or no call was
issued yet.
.. method:: Cursor.fetchmany(size=None)
Fetch the next batch of rows of a query result, returning a list of tuples,
or a list of dictionaries if *as_dict* was passed to
:func:`pymssql.connect()`, or an empty list if no more data is available. You
can adjust the batch size using the *size* parameter, which is preserved
across many calls to this method. Raises ``OperationalError``
(:pep:`249#operationalerror`) if previous call to ``execute*()`` did not
produce any result set or no call was issued yet.
.. method:: Cursor.fetchall()
Fetch all remaining rows of a query result, returning a list of tuples, or a
list of dictionaries if as_dict was passed to ``pymssql.connect()``, or an
empty list if no more data is available. Raises ``OperationalError``
(:pep:`249#operationalerror`) if previous call to ``execute*()`` did not
produce any result set or no call was issued yet.
.. method:: Cursor.nextset()
This method makes the cursor skip to the next available result set,
discarding any remaining rows from the current set. Returns ``True`` value if
next result is available, ``None`` if not.
.. method:: Cursor.__iter__()
Cursor.next()
These methods facilitate :ref:`Python iterator protocol <python:typeiter>`.
You most likely will not call them directly, but indirectly by using
iterators.
A pymssql extension to the DB-API 2.0.
.. method:: Cursor.setinputsizes()
Cursor.setoutputsize()
These methods do nothing, as permitted by DB-API specs.
Exceptions
==========
.. exception:: StandardError
Root of the exception hierarchy.
.. exception:: Warning
Raised for important warnings like data truncations while inserting, etc. A
subclass of :exc:`StandardError`.
.. exception:: Error
Base class of all other error exceptions. You can use this to catch all
errors with one single except statement. A subclass of :exc:`StandardError`.
.. exception:: InterfaceError
Raised for errors that are related to the database interface rather than the
database itself. A subclass of :exc:`Error`.
.. exception:: DatabaseError
Raised for errors that are related to the database. A subclass of
:exc:`Error`.
.. exception:: DataError
Raised for errors that are due to problems with the processed data like
division by zero, numeric value out of range, etc. A subclass of
:exc:`DatabaseError`.
.. exception:: OperationalError
Raised for errors that are related to the database's operation and not
necessarily under the control of the programmer, e.g. an unexpected
disconnect occurs, the data source name is not found, a transaction could
not be processed, a memory allocation error occurred during processing, etc.
A subclass of :exc:`DatabaseError`.
.. exception:: IntegrityError
Raised when the relational integrity of the database is affected, e.g. a
foreign key check fails. A subclass of :exc:`DatabaseError`.
.. exception:: InternalError
Raised when the database encounters an internal error, e.g. the cursor is
not valid anymore, the transaction is out of sync, etc. A subclass of
:exc:`DatabaseError`.
.. exception:: ProgrammingError
Raised for programming errors, e.g. table not found or already exists,
syntax error in the SQL statement, wrong number of parameters specified,
etc. A subclass of :exc:`DatabaseError`.
.. exception:: NotSupportedError
Raised in case a method or database API was used which is not supported by
the database, e.g. requesting a :meth:`~Connection.rollback()` on a
connection that does not support transaction or has transactions turned off.
A subclass of :exc:`DatabaseError`.
.. exception:: ColumnsWithoutNamesError
Raised by :meth:`Cursor.execute` when ``as_dict=True`` has been specified
to :func:`open <connect>` the :class:`connection <Connection>` and the
query sent to the server doesn't involve columns names in its results.
A subclass of :exc:`InterfaceError`.
.. note::
``ColumnsWithoutNamesError`` isn't a PEP-249-mandated exception but
rather a pymssql extension.
.. _SQL Injection attacks: http://en.wikipedia.org/wiki/SQL_injection
|