Python mssql2 PDF
Python mssql2 PDF
Python mssql2 PDF
import pymssql
conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase')
cur = conn.cursor()
cur.execute('CREATE TABLE persons(id INT, name VARCHAR(100))')
cur.executemany("INSERT INTO persons VALUES(%d, %s)", \
[ (1, 'John Doe'), (2, 'Jane Doe') ])
conn.commit() # you must call commit() to persist your data if you don't set autocommit to True
# if you call execute() with one argument, you can use % sign as usual
# (it loses its special meaning).
cur.execute("SELECT * FROM persons WHERE salesrep LIKE 'J%'")
conn.close()
You can also use iterators instead of while loop. Iterators are DB-API extensions, and are
available since pymssql 1.0.
Rows as dictionaries
Since pymssql 1.0.2 rows can be fetched as dictionaries instead of tuples. This allows
for accessing columns by name instead of index.
import pymssql
conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase',as
_dict=True)
cur = conn.cursor()
conn.close()
_mssql examples (no DB-API overhead)
This module allows for easy communication with SQL Server.
import _mssql
conn = _mssql.connect(server='SQL01', user='user', password='password', \
database='mydatabase')
conn.execute_non_query('CREATE TABLE persons(id INT, name VARCHAR(100))')
conn.execute_non_query("INSERT INTO persons VALUES(1, 'John Doe')")
conn.execute_non_query("INSERT INTO persons VALUES(2, 'Jane Doe')")
conn.close()
Please note the usage of iterators and ability to access results by column name. Also please note
that parameters to connect method have different names than in pymssql module.
import _mssql
try:
conn = _mssql.connect(server='SQL01', user='user', password='password', \
database='mydatabase')
conn.execute_non_query('CREATE TABLE t1(id INT, name VARCHAR(50))')
except _mssql.MssqlDatabaseException,e:
if e.number == 2714 and e.severity == 16:
# table already existed, so quieten the error
else:
raise # re-raise real error
finally:
conn.close()
pymssql on Windows
pymssql on Windows doesn't require any additional components to be installed. The
only required library, ntwdblib.dll, is included with pymssql, and it's all that is needed
to make connections to Microsoft SQL Servers. It is called DB Libary for C, and is
documented here. This library is responsible for low level communication with SQL
Servers, and is extensively used by _mssql module.
Typically you don't need any configuration in order to make it work. However I wrote a
few paragraphs about more sophisticated configuration in Advanced information.
On Windows in addition to authenticating to SQL using user name and password, you
can also authenticate using so called Windows Authentication, or Trusted Connection.
In this mode SQL Server validates the connection using user's security token provided
by the operating system. In other words, user's identity is confirmed by Windows, so
SQL trusts it (hence trusted connection). It is suggested method of connecting,
because is eliminates the need for hard coding passwords in clear text in scripts.
pymssql on Linux/*nix
Linux/*nix on this webpage refers to any operating system different than Microsoft
Windows, i.e. Linux, BSD, Solaris, MacOS etc.
pymssql on these platforms require a low level driver that is able to speak to SQL
Servers. This driver is implemented by FreeTDS package. FreeTDS can speak to both
Microsoft SQL Servers and Sybase servers, however there is a problem with dates,
which is described in more details on FreeTDS and Dates page.
You will need to configure FreeTDS. It needs nonempty configuration in order to work.
Below you will find a simple examples, but for more sophisticated configurations you
will need to consult FreeTDS User Guide.
You can connect using explicit host name and port number:
pymssqlCnx class
This class represents an MS SQL database connection. You can create an instance of
this class by calling constructor pymssql.connect(). It accepts following arguments.
Note that in most cases you will want to use keyword arguments, instead of positional
arguments.
dsn
colon-delimited string of the form host:dbase:user:pass:opt:tty, primarily for
compatibility with previous versions of pymssql.
user
database user to connect as.
password
user's password.
trusted
bolean value signalling whether to use Windows Integrated Authentication to connect
instead of SQL autentication with user and password [Windows only]
host
database host and instance you want to connect to. Valid examples are:
database
the database you want initially to connect to, by default SQL Server selects the
database which is set as default for specific user.
timeout
query timeout in seconds, default is 0 (wait indefinitely).
login_timeout
timeout for connection and login in seconds, default 60.
charset
character set with which to connect to the database.
as_dict
whether rows should be returned as dictionaries instead of tuples (added in pymssql
1.0.2).
max_conn
how many simultaneous connections to allow; default is 25, maximum on Windows is
1474559; trying to set it to higher value results in error 'Attempt to set maximum
number of DBPROCESSes lower than 1.' (error 10073 severity 7) (added in pymssql
1.0.2).
autocommit(status)
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.
close()
Close the connection.
cursor()
Return a cursor object, that can be used to make queries and fetch results from the
database.
commit()
Commit current transaction. You must call this method to persist your data if
you leave autocommit at its default value, which isFalse. See also pymssql example at
the top of this page.
rollback()
Roll back current transaction.
pymssqlCursor class
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 pymssqlCursor
instances by calling cursor() method on an open pymssqlCnx connection object.
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.
connection
This is the extension of the DB-API specification. Returns a reference to the connection
object on which the cursor was created.
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.
rownumber
This is the extension of the DB-API specification. Returns current 0-based index of the
cursor in the result set.
close()
Close the cursor. The cursor is unusable from this point.
execute(operation)
execute(operation, params)
operation is a string and params, if specified, is a simple value, a tuple, 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 the same formatting as Python's builtin string
interpolation operator.
If you call execute() with one argument, you can use % sign as usual in your query
string, for example in LIKE operator (it loses its special meaning). See the example at
the top.
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.
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 if previous call to execute*() did not produce any result set or
no call was issued yet.
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 topymssql.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 if previous call
to execute*() did not produce any result set or no call was issued yet.
fetchall()
Fetch all remaining rows of a query result, returning a list of tuples, or a list of
dictionaries if as_dict was passed topymssql.connect(), or an empty list if no more
data is available. Raises OperationalError if previous call to execute*() did not produce
any result set or no call was issued yet.
fetchone_asdict()
Warning: this method is not part of DB-API. This method is deprecated as of pymsssql
1.0.2. It was replaced by as_dict parameter topymssql.connect()
Fetch the next row of a query result, returning a dictionary, or None if no more data is
available. Data can be accessed by 0-based numeric column index, or by column
name. Raises OperationalError if previous call to execute*() did not produce any result
set or no call was issued yet.
fetchmany_asdict(size=None)
Warning: this method is not part of DB-API. This method is deprecated as of pymsssql
1.0.2. It was replaced by as_dict parameter topymssql.connect()
Fetch the next batch of rows of a query result, returning a list of dictionaries. An
empty list is returned if no more data is available. Data can be accessed by 0-based
numeric column index, or by column name. You can adjust the batch size using
the size parameter, which is preserved across many calls to this method.
Raises OperationalError if previous call to execute*() did not produce any result set or
no call was issued yet.
fetchall_asdict()
Warning: this method is not part of DB-API. This method is deprecated as of pymsssql
1.0.2. It was replaced by as_dict parameter topymssql.connect()
Fetch all remaining rows of a query result, returning a list of dictionaries. An empty list
is returned if no more data is available. Data can be accessed by 0-based numeric
column index, or by column name. Raises OperationalError if previous call
to execute*() did not produce any result set or no call was issued yet.
The idea and original implementation of this method by Sterling Michel
<sterlingmichel_at_gmail_dot_com>
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.
__iter__(), next()
These methods faciliate Python iterator protocol. You most likely will not call them
directly, but indirectly by using iterators.
setinputsizes(), setoutputsize()
These methods do nothing, as permitted by DB-API specs.
_mssql module reference
min_error_severity
Minimum severity of errors at which to begin raising exceptions. The default value of 6
should be appropriate in most cases.
MssqlConnection class
This class represents an MS SQL database connection. You can make queries and
obtain results through a database connection.
server
database server and instance you want to connect to. Valid examples are:
user
database user to connect as.
password
user's password.
trusted
bolean value signalling whether to use Windows Integrated Authentication to connect
instead of SQL autentication with user and password [Windows only]
charset
character set name to set for the connection.
database
the database you want initially to connect to, by default SQL Server selects the
database which is set as default for specific user.
max_conn
how many simultaneous connections to allow; default is 25, maximum on Windows is
1474559; trying to set it to higher value results in error 'Attempt to set maximum
number of DBPROCESSes lower than 1.' (error 10073 severity 7) (added in pymssql
1.0.2).
connected
True if the connection object has an open connection to a database, false otherwise.
charset
Character set name that was passed to _mssql.connect() method.
identity
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. Example usage --
assume that 'persons' table contains an identity column in addition to 'name' column:
query_timeout
Query timeout in seconds, default is 0, what means to wait indefinitely for results. Due
to the way DB-Library for C works, setting this property affects all connections opened
from current Python script (or, very technically, all connections made from this
instance ofdbinit()).
rows_affected
Number of rows affected by last query. For SELECT statements this value is only
meaningful after reading all rows.
debug_queries
If set to true, all queries are printed to stderr after formatting and quoting, just before
being sent to SQL Server. It may be helpful if you suspect problems with formatting or
quoting.
cancel()
Cancel all pending results from the last SQL operation. It can be called more than one
time in a row. No exception is raised in this case.
close()
Close the connection and free all memory used. It can be called more than one time in
a row. No exception is raised in this case.
execute_query(query_string)
execute_query(query_string, params)
This method sends a query to the MS SQL Server to which this object instance is
connected. An exception is raised on failure. If there are pending results or rows prior
to executing this command, they are silently discarded. After calling this method you
may iterate over the connection object to get rows returned by the query. You can use
Python formatting and all values get properly quoted. Please see examples at the top
of this page for details. This method is intented to be used on queries that return
results, i.e. SELECT.
execute_non_query(query_string)
execute_non_query(query_string, params)
This method sends a query to the MS SQL Server to which this object instance is
connected. After completion, its results (if any) are discarded. An exception is raised
on failure. If there are pending results or rows prior to executing this command, they
are silently discarded. You can use Python formatting and all values get properly
quoted. Please see examples at the top of this page for details. This method is useful
for INSERT, UPDATE, DELETE, and for Data Definition Language commands, i.e. when
you need to alter your database schema.
execute_scalar(query_string)
execute_scalar(query_string, params)
This method sends a query to the MS SQL Server to which this object instance is
connected, then returns first column of first row from result. An exception is raised on
failure. If there are pending results or rows prior to executing this command, they are
silently discarded. You can use Python formatting and all values get properly quoted.
Please see examples at the top of this page for details. This method is useful if you
want just a single value from a query, as in the example below. This method works in
the same way asiter(conn).next()[0]. Remaining rows, if any, can still be iterated after
calling this method. Example usage:
execute_row(query_string)
execute_row(query_string, params)
This method sends a query to the MS SQL Server to which this object instance is
connected, then returns first row of data from result. An exception is raised on failure.
If there are pending results or rows prior to executing this command, they are silently
discarded. You can use Python formatting and all values get properly quoted. Please
see examples at the top of this page for details. This method is useful if you want just
a single row and don't want or don't need to iterate over the connection object. This
method works in the same way as iter(conn).next() to obtain single row. Remaining
rows, if any, can still be iterated after calling this method. Example usage:
get_header()
This method is infrastructure and don't need to be called by your code. Get the Python
DB-API compliant header information. Returns a list of 7-element tuples describing
current result header. Only name and DB-API compliant type is filled, rest of the data
is None, as permitted by the specs.
nextresult()
Move to the next result, skipping all pending rows. This method fetches and discards
any rows remaining from current operation, then it advances to next result (if any).
Returns True value if next set is available, None otherwise. An exception is raised on
failure.
select_db(dbname)
This function makes given database the current one. An exception is raised on failure.
__iter__(), next()
These methods faciliate Python iterator protocol. You most likely will not call them
directly, but indirectly by using iterators.
MssqlException
|
+-- MssqlDriverException
|
+-- MssqlDatabaseException
number
The error code, as returned by SQL Server.
severity
The so-called severity level, as returned by SQL Server. If value of this property is less
than the value of_mssql.min_error_severity, such errors are ignored and exceptions
are not raised.
state
The third error code, as returned by SQL Server.
message
The error message, as returned by SQL Server.
You can find an example of how to use this data at the bottom of _mssql
examples page.
Frequently asked questions / troubleshooting page
Cannot connect to SQL Server.
If you can't connect to the SQL Server instance, try the following:
• by default SQL Server 2005 and newer doesn't accept remote connections, you
have to use SQL Server Surface Area Configuration and/or SQL Server
Configuration Manager to enable specific protocols and network adapters;
don't forget to restart SQL Server after making these changes,
• if you use pymssql on Windows and the server is on local machine, you can try
the following command from the command prompt:
REG ADD HKLM\Software\Microsoft\MSSQLServer\Client /v SharedMemoryOn /t
REG_DWORD /d 1 /f
It means that SQL Server is unable to send Unicode data to pymssql, because of
shortcomings of DB-Library for C. You have to CAST or CONVERT the data to
equivalent NVARCHAR data type, which does not exhibit this behaviour.
It may mean that FreeTDS library is unavailable, or that dynamic linker is unable to
find it. Check that it is installed and that the path to libsybdb.so is
in /etc/ld.so.conf file. Then do ldconfig as root to refresh linker database. On Solaris I
just set LD_LIBRARY_PATH environment variable to directory with the library just
before launching Python.
"DB-Lib error message 20004, severity 9: Read from SQL server failed" error
appears.
On Linux/*nix you may encounter the following behaviour:
>>> import _mssql
>>> c=_mssql.connect('hostname:portnumber','user','pass')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
_mssql.DatabaseException: DB-Lib error message 20004, severity 9:
Read from SQL server failed.
DB-Lib error message 20014, severity 9:
Login incorrect.
"Login incorrect" following this error is spurious, real "Login incorrect" messages has
code=18456 and severity=14.
Python on Windows dies with memory access violation error on connect() when
incorrect password is given.
This may happen if you use different version of ntwdblib.dll than the one included in
pymssql package. For example the version 2000.80.2273.0 is unable to
handle dberrhandle() callbacks properly, and causes access violation error
in err_handler() function on return INT_CANCEL. I have given up after several hours of
investigating the issue, and just reverted to previous version of the ntwdblib.dll and
error disappeared.
This may happen most likely on earlier versions of pymssql. It happens always if you
use a colon ":" to separate hostname from port number. On Windows you should use
comma "," instead. pymssql 1.0 has a workaround, so you do not have to care about
that.
More troubleshooting.
If the above hasn't covered the problem, please also check Limitations and known
issues page. You can also consult FreeTDS troubleshooting page for issues related to
the TDS protocol.
Limitations and known issues
DB-Library for C is not supported by Microsoft any more. You can find the info on this
MSDN page. This is why some of the features may not work as expected. Here are
some known issues and workarounds. You should note that none of these issues are
imposed by pymssql. You will observe them all also in PHP driver for MSSQL, for
instance.
You should avoid NULL bit fields. Just assign a default value to them and
update all records to the default value. I would recommend not
using BITdatatype at all, if possible change it to TINYINT for example. The
problem will disappear, and storage overhead is unnoticeable. This issue is
also known for example in Microsoft's own product, Access,
see KB278696 article.
• New features of SQL Server 2005 and SQL Server 2008 are not
supported.
Some of the features of SQL Server versions newer than 2000 just don't work.
For example you can't use MARS feature (Multiple Active Result Sets). You have
to arrange your queries so that result sets are fetched one after another.
FreeTDS, which is used on Linux/*nix to connect to Sybase and MS SQL servers, tries
to imitate both modes:
If you can do nothing about FreeTDS, there's a workaround. You can redesign your
queries to return string instead of bare date:
SELECT datecolumn FROM tablename
can be rewritten into:
SELECT CONVERT(CHAR(10),datecolumn,120) AS datecolumn FROM tablename
This way SQL will send you string representing the date instead of binary date in
datetime or smalldatetime format, which has to be processed by FreeTDS and
pymssql.
REG ADD
"HKLM\Software\Microsoft\MSSQLServer\Client\ConnectTo
" /v sqlhost3 /t REG_SZ /d
"DBNMPNTW,\\sqlhost3\pipe\sql\query"
then from pymssql connect as usual, giving just the string 'sqlhost3' as
host parameter to connect() method. This way you only provide the
alias sqlhost3 and the driver looks for the settings in Registry. The above
path \\sqlhost3\pipe\sql\query usually means default SQL Server instance
on sqlhost3 machine. You have to consult your configuration (either with
Server Network Utility or SQL Server Configuration Manager) to obtain
path for a named instance.
REG ADD
"HKLM\Software\Microsoft\MSSQLServer\Client\ConnectTo
" /v sqlhost4 /t REG_SZ /d
"DBMSSOCN,sqlhost4.example.com,1433"
then from pymssql connect as usual, giving just the string 'sqlhost4' as
host parameter to connect() method. This way you only provide the
alias sqlhost4 and the driver looks for the settings in Registry. As you can
see, there is host name and TCP port number hard coded for the
alias sqlhost4.
Compilation and installation from source
If you need to compile pymssql, check whether requirements shown below are met, unpack
source files to a directory of your choice and issue (as root):
# python setup.py install
This will compile and install pymssql.
Build Requirements
Platform-specific issues
Mandriva Linux
If you use some older versions of Mandriva Linux and want to compile pymssql, you
may have to edit setup.py and change:
libraries = ["sybdb"]
into:
libraries = ["sybdb_mssql"]
Be sure to install libfreetds_mssql0 package first.
Windows
FreeTDS on Windows is not supported.