与 mssql 和 pyodbc 的只读连接
Readonly connection with mssql and pyodbc
我在尝试创建只读连接时遇到了一些问题,不确定这是错误还是我的错误。
Pyodbc 的文档表明可以创建只读连接。参见 https://mkleehammer.github.io/pyodbc/api-module.html
当 运行 以下虽然我没有收到任何错误并且它运行时就好像 READONLY 关键字根本不存在一样(更新通过)。
import pyodbc
readonly_conn_str = "DRIVER={SQL Server Native Client 10.0};SERVER=...;DATABASE=...;UID=...;PWD=...;READONLY=True;"
conn = pyodbc.connect(readonly_conn_str)
cursor = conn.cursor()
result = cursor.execute(update_query)
cursor.commit()
print(result.rowcount)
如果我尝试使用函数关键字,我会得到同样的结果。
...
conn = pyodbc.connect(conn_str, readonly=True)
...
当我们用 readonly=True
调用 pyodbc.connect
时,pyodbc 尽职尽责地调用 ODBC 函数
ret = SQLSetConnectAttr(cnxn->hdbc, SQL_ATTR_ACCESS_MODE, (SQLPOINTER)SQL_MODE_READ_ONLY, 0);
SQL_MODE_READ_ONLY
是标准 ODBC SQL_ATTR_ACCESS_MODE
属性之一。这实际上如何影响 ODBC 连接的行为取决于 ODBC 驱动程序。如 ODBC documentation 中所述:
SQL_MODE_READ_ONLY is used by the driver or data source as an indicator that the connection is not required to support SQL statements that cause updates to occur. This mode can be used to optimize locking strategies, transaction management, or other areas as appropriate to the driver or data source. The driver is not required to prevent such statements from being submitted to the data source. The behavior of the driver and data source when asked to process SQL statements that are not read-only during a read-only connection is implementation-defined.
换句话说,pyodbc 已经将 "read only" 属性传递给了 ODBC 驱动程序。由驱动程序决定是否应将其解释为提示、硬限制或简单忽略的内容。
我在尝试创建只读连接时遇到了一些问题,不确定这是错误还是我的错误。
Pyodbc 的文档表明可以创建只读连接。参见 https://mkleehammer.github.io/pyodbc/api-module.html
当 运行 以下虽然我没有收到任何错误并且它运行时就好像 READONLY 关键字根本不存在一样(更新通过)。
import pyodbc
readonly_conn_str = "DRIVER={SQL Server Native Client 10.0};SERVER=...;DATABASE=...;UID=...;PWD=...;READONLY=True;"
conn = pyodbc.connect(readonly_conn_str)
cursor = conn.cursor()
result = cursor.execute(update_query)
cursor.commit()
print(result.rowcount)
如果我尝试使用函数关键字,我会得到同样的结果。
...
conn = pyodbc.connect(conn_str, readonly=True)
...
当我们用 readonly=True
调用 pyodbc.connect
时,pyodbc 尽职尽责地调用 ODBC 函数
ret = SQLSetConnectAttr(cnxn->hdbc, SQL_ATTR_ACCESS_MODE, (SQLPOINTER)SQL_MODE_READ_ONLY, 0);
SQL_MODE_READ_ONLY
是标准 ODBC SQL_ATTR_ACCESS_MODE
属性之一。这实际上如何影响 ODBC 连接的行为取决于 ODBC 驱动程序。如 ODBC documentation 中所述:
SQL_MODE_READ_ONLY is used by the driver or data source as an indicator that the connection is not required to support SQL statements that cause updates to occur. This mode can be used to optimize locking strategies, transaction management, or other areas as appropriate to the driver or data source. The driver is not required to prevent such statements from being submitted to the data source. The behavior of the driver and data source when asked to process SQL statements that are not read-only during a read-only connection is implementation-defined.
换句话说,pyodbc 已经将 "read only" 属性传递给了 ODBC 驱动程序。由驱动程序决定是否应将其解释为提示、硬限制或简单忽略的内容。