_mssql
module referencepymssql模块类,方法和属性的完整文档。
Complete documentation of _mssql
module classes, methods and properties.
模块全局变量
Variables whose values you can change to alter behavior on a global basis.
_mssql.
login_timeout
Timeout for connection and login in seconds, default 60.
_mssql.
min_error_severity
Minimum severity of errors at which to begin raising exceptions. The default value of 6 should be appropriate in most cases.
_mssql.
set_max_connections
(number)Sets maximum number of simultaneous connections allowed to be open at any given time. Default is 25.
_mssql.
get_max_connections
()Gets current maximum number of simultaneous connections allowed to be open at any given time.
MSSQLConnection
class_mssql.
MSSQLConnection
此类表示MS SQL数据库连接。您可以查询并获得通过一个数据库连接的结果。
你可以通过调用pymssql.connect创建这个类的一个实例()。它接受以下参数。请注意,您可以使用关键字参数,而不是位置参数
This class represents an MS SQL database connection. You can make queries and obtain results through a database connection.
You can create an instance of this class by calling _mssql.connect()
. It accepts the following arguments. Note that you can use keyword arguments, instead of positional arguments.
Parameters: |
|
---|
SET ARITHABORT ON;
SET CONCAT_NULL_YIELDS_NULL ON;
SET ANSI_NULLS ON;
SET ANSI_NULL_DFLT_ON ON;
SET ANSI_PADDING ON;
SET ANSI_WARNINGS ON;
SET ANSI_NULL_DFLT_ON ON;
SET CURSOR_CLOSE_ON_COMMIT ON;
SET QUOTED_IDENTIFIER ON;
SET TEXTSIZE 2147483647; -- http://msdn.microsoft.com/en-us/library/aa259190%28v=sql.80%29.aspx
New in version 2.1.1: The conn_properties parameter. 2.1.1新版本连接参数
2.1.1版本更改:2.1.1之前,没有byconn属性指定的初始化查询是不是定制的,它的值被硬编码为上面显示的文字。
Changed in version 2.1.1: Before 2.1.1, the initialization queries now specified byconn_properties wasn’t customizable and its value was hard-coded to the literal shown above.
Note 注意
If you need to connect to Azure: 如果您需要连接到Azure
New in version 2.1.1: The ability to connect to Azure.新版本2.1.1可连接到AZURE
2.2.0更新(这里是有错误吗,觉得版本号不对.不是才2.1.1吗,怎么这里讲2.2.0了呢),tds_version这个参数默认值是None.2.0.0到2.1.2默认值为7.1
Changed in version 2.2.0: The default value of the tds_version parameter was changed to None
. Between versions 2.0.0 and 2.1.2 its default value was ‘7.1‘
.
Warning警告
该tds_version参数有没有一个默认值。这意味着两件事情:
The tds_version parameter has a default value of None
. This means two things:
‘7.1‘
default value and (不能再依靠旧值7.1)这可能看起来繁琐,但同时意味着使用pymssql/ pymssql没有在freetds.conf文件甚至没有freetds.conf在所有使用的节服务器时,你可以现在已完全配置SQL Server的连接的特性。与pymssql版本2.0.0及以上开始2.1.2版已经是可以设置TDS协议版本要求连接到服务器时,但如果没有指定使用的版本7.1。
This might look cumbersome but at the same time means you can now fully configure the characteristics of a connection to SQL Server when using pymssql/_mssql without using a stanza for the server in the freetds.conf
file or even with no freetds.conf
at all. Starting with pymssql version 2.0.0 and up to version 2.1.2 it was already possible to set the TDS protocol version to ask for when connecting to the server but version 7.1 was used if not specified.
MSSQLConnection
object properties SQLConnection对象的属性MSSQLConnection.
connected
True
if the connection object has an open connection to a database, False
otherwise.
MSSQLConnection.
charset
Character set name that was passed to _mssql.connect().
MSSQLConnection.
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:
conn.execute_non_query("INSERT INTO persons (name) VALUES(‘John Doe‘)")# 插入一条数据,ID为自增长的
print "Last inserted row has id = " + conn.identity # 得到这个自增长ID值
MSSQLConnection.
query_timeout
Query timeout in seconds, default is 0, which means to wait indefinitely for results. Due to the way DB-Library for C works, setting this property affects all connections opened from the current Python script (or, very technically, all connections made from this instance of dbinit()).
MSSQLConnection.
rows_affected
Number of rows affected by last query. For SELECT statements this value is only meaningful after reading all rows.
MSSQLConnection.
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.
MSSQLConnection.
tds_version
此连接使用的TDS版本。可以是4.2的,5.07.0,7.1和7.2。
2.2.0版本有改变:正确性和一致性用于指示从8.0改为7.1 pymssql2.2.0 TDS7.1的值。(这里的版本2.2.0也可能有误)
The TDS version used by this connection. Can be one of 4.2
, 5.0
7.0
, 7.1
and 7.2
.
Changed in version 2.2.0: For correctness and consistency the value used to indicate TDS 7.1 changed from 8.0
to 7.1
on pymssql 2.2.0.
MSSQLConnection
object methods 方法MSSQLConnection.
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.
MSSQLConnection.
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.
MSSQLConnection.
execute_query
(query_string)MSSQLConnection.
execute_query
(query_string, params) 此方法发送一个查询向此对象实例所连接的MS SQL Server中。将引发异常的故障。如果没有等待执行此命令之前结果或行,它们丢弃。
调用此方法后你可能会遍历连接对象来获取查询返回的行。
您可以使用Python的格式和所有价值得到正确引用。详情请参阅例子。
这种方法的目的是要在返回结果的查询,即选择使用。
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 for details.
This method is intented to be used on queries that return results, i.e. SELECT.
MSSQLConnection.
execute_non_query
(query_string)MSSQLConnection.
execute_non_query
(query_string, params)此方法发送一个查询向此对象实例所连接的MS SQL Server中。完成后,其结果(如果有的话)被丢弃。将引发异常的故障。如果没有等待执行此命令之前结果或行,它们丢弃。
您可以使用Python的格式和所有价值得到正确引用。详情请参阅例子。
这种方法对于INSERT,UPDATE,DELETE,以及用于数据定义语言命令,即,当你需要改变你的数据库架构。
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 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.
MSSQLConnection.
execute_scalar
(query_string)MSSQLConnection.
execute_scalar
(query_string, params)该方法发送一个查询到该对象实例所连接的MS SQL服务器,然后从结果返回第一行的第一列中。将引发异常的故障。如果没有等待执行此命令之前结果或行,它们丢弃。
您可以使用Python的格式和所有价值得到正确引用。详情请参阅例子。
如果希望从一个查询只是一个单一的值,如在下面的例子中该方法是有用的。这种方法以同样的方式工作原理ITER(conn)的的.next()[0]。剩余的行中,如果有的话,仍然可以调用此方法后迭代。
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 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 as iter(conn).next()[0]
. Remaining rows, if any, can still be iterated after calling this method.
Example usage:
count = conn.execute_scalar("SELECT COUNT(*) FROM employees")
MSSQLConnection.
execute_row
(query_string)MSSQLConnection.
execute_row
(query_string, params)该方法发送一个查询到该对象实例所连接的MS SQL服务器,然后返回从结果数据的第一行。将引发异常的故障。如果没有等待执行此命令之前结果或行,它们丢弃。
您可以使用Python的格式和所有价值得到正确引用。详情请参阅例子。
如果你想要一个单行,不想或不需要遍历连接对象此方法非常有用。此方法适用于相同的方式ITER(conn)的的.next(),以获得单列。剩余的行中,如果有的话,仍然可以调用此方法后迭代。
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 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:
empinfo = conn.execute_row("SELECT * FROM employees WHERE empid=10")
MSSQLConnection.
get_header
()This method is infrastructure and doesn’t need to be called by your code. It gets 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.
MSSQLConnection.
init_procedure
(name)Create an MSSQLStoredProcedure object that will be used to invoke thestored procedure with the given name.
MSSQLConnection.
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.
MSSQLConnection.
select_db
(dbname)This function makes the given database the current one. An exception is raised on failure.
MSSQLConnection.
__iter__
()MSSQLConnection.
next
()New in version 2.1.0.版本新增
这些方法实现了Python迭代器协议。你很可能不会使用迭代器直接调用它们,而是间接的。
These methods implement the Python iterator protocol. You most likely will not call them directly, but indirectly by using iterators.
MSSQLConnection.
set_msghandler
(handler)New in version 2.1.1.版本新增
此方法允许设置一个消息处理功能的连接,以允许客户端来访问从服务器返回的消息。
传递给此方法消息处理函数处理程序的签名必须是:
This method allows setting a message handler function for the connection to allow a client to gain access to the messages returned from the server.
The signature of the message handler function handler passed to this method must be:
def my_msg_handler(msgstate, severity, srvname, procname, line, msgtext):
# The body of the message handler.
信息状态,严重程度和行号是数值,服务器,过程和查询是字符串
msgstate, severity and line will be integers, srvname, procname and msgtext will be strings.
MSSQLStoredProcedure
class 存储过程类_mssql.
MSSQLStoredProcedure
This class represents a stored procedure. You create an object of this class by calling theinit_procedure()
method on MSSQLConnection
object.
MSSQLStoredProcedure
object properties 存储过程对象的属性MSSQLStoredProcedure.
connection
An underlying MSSQLConnection object. 基本的MSSQLConnection连接对象
MSSQLStoredProcedure.
name
The name of the procedure that this object represents.代表存储过程对象的名字
MSSQLStoredProcedure.
parameters
The parameters that have been bound to this procedure.绑定到该存储过程的参数
MSSQLStoredProcedure
object methods这个方法绑定一个参数,value和dbtype必须
MSSQLStoredProcedure.
bind
(value, dbtype, name=None, output=False, null=False, max_length=-1)This method binds a parameter to the stored procedure. value and dbtype are mandatory arguments, the rest is optional.
Parameters: |
|
---|
MSSQLStoredProcedure.
execute
()Execute the stored procedure.
Exception hierarchy:
MSSQLException
|
+-- MSSQLDriverException
|
+-- MSSQLDatabaseException
_mssql.
MSSQLDriverException
MSSQLDriverException
is raised whenever there is a problem within _mssql
– e.g. insufficient memory for data structures, and so on.
_mssql.
MSSQLDatabaseException
MSSQLDatabaseException
is raised whenever there is a problem with the database – e.g. query syntax error, invalid object name and so on. In this case you can use the following properties to access details of the error:
number
The error code, as returned by SQL Server.错误代码,由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.第三个错误代码,由SQL Server返回。
message
The error message, as returned by SQL Server.错误信息,如SQL Server返回。
你可以找到如何在pymssql例子页面的底部使用这些数据的例子。
You can find an example of how to use this data at the bottom of _mssql examples page.
原文:http://www.cnblogs.com/mirrortom/p/5223963.html