仅 pyodbc 行数 returns -1
pyodbc rowcount only returns -1
行数是如何工作的。我正在使用 pyodbc,它总是返回 -1。
return_query = conn.query_db_param(query, q_params)
print(return_query.rowcount)
def query_db_param(self, query, params):
self.cursor.execute(query,params)
print(self.cursor.rowcount)
rowcount
指的是上次操作影响的行数。所以,如果你执行 insert
并且只插入一行,那么它将 return 1。如果你更新 200 行,那么它将 return 200。另一方面,如果你SELECT
,最后一个操作并没有真正影响rows,它是一个结果集。在这种情况下,0
在句法上是不正确的,因此接口 return 改为 -1
。
对于设置变量或使用 create/alter 命令等操作,它也会 return -1
。
您正在连接的数据库无法为您的查询提供该号码。许多数据库引擎在您获取结果时生成行,扫描它们的内部 table 和索引数据结构以获取下一个匹配结果。在您获取所有行之前,引擎无法知道最终计数。
当行数未知时,Python DB-API 2.0 specification for Cursor.rowcount
指出在这种情况下必须将数字设置为 -1
:
The attribute is -1
in case [...] the rowcount of the last operation is cannot be determined by the interface.
pyodbc Cursor.rowcount
documentation 符合这个要求:
The number of rows modified by the last SQL statement.
This is -1 if no SQL has been executed or if the number of rows is unknown. Note that it is not uncommon for databases to report -1 immediately after a SQL select statement for performance reasons. (The exact number may not be known before the first records are returned to the application.)
pyodbc 在这方面并不孤单,另一个易于 link 的例子是 Python 标准库 sqlite3
模块;它是 Cursor.rowcount
documentation 状态:
As required by the Python DB API Spec, the rowcount attribute “is -1 in case no executeXX()
has been performed on the cursor or the rowcount of the last operation is not determinable by the interface”. This includes SELECT
statements because we cannot determine the number of rows a query produced until all rows were fetched.
请注意,对于数据库实现的子集,可以在获取 一些 行后更新行计数值。您必须检查您正在连接的特定数据库文档,以查看该实现是否可以执行此操作,或者行数是否必须保持为 -1。当然,您可以随时尝试。
你可以先执行COUNT()
select,或者,如果结果集预计不会太大,使用cursor.fetchall()
,然后使用len()
结果列表。
如果您使用的是微软 sql 服务器,并且您想要获取先前 select 语句中返回的行数,您可以只执行 select @@rowcount
.
例如:
cursor.execute("select @@rowcount")
rowcount = cursor.fetchall()[0][0]
行数是如何工作的。我正在使用 pyodbc,它总是返回 -1。
return_query = conn.query_db_param(query, q_params)
print(return_query.rowcount)
def query_db_param(self, query, params):
self.cursor.execute(query,params)
print(self.cursor.rowcount)
rowcount
指的是上次操作影响的行数。所以,如果你执行 insert
并且只插入一行,那么它将 return 1。如果你更新 200 行,那么它将 return 200。另一方面,如果你SELECT
,最后一个操作并没有真正影响rows,它是一个结果集。在这种情况下,0
在句法上是不正确的,因此接口 return 改为 -1
。
对于设置变量或使用 create/alter 命令等操作,它也会 return -1
。
您正在连接的数据库无法为您的查询提供该号码。许多数据库引擎在您获取结果时生成行,扫描它们的内部 table 和索引数据结构以获取下一个匹配结果。在您获取所有行之前,引擎无法知道最终计数。
当行数未知时,Python DB-API 2.0 specification for Cursor.rowcount
指出在这种情况下必须将数字设置为 -1
:
The attribute is
-1
in case [...] the rowcount of the last operation is cannot be determined by the interface.
pyodbc Cursor.rowcount
documentation 符合这个要求:
The number of rows modified by the last SQL statement.
This is -1 if no SQL has been executed or if the number of rows is unknown. Note that it is not uncommon for databases to report -1 immediately after a SQL select statement for performance reasons. (The exact number may not be known before the first records are returned to the application.)
pyodbc 在这方面并不孤单,另一个易于 link 的例子是 Python 标准库 sqlite3
模块;它是 Cursor.rowcount
documentation 状态:
As required by the Python DB API Spec, the rowcount attribute “is -1 in case no
executeXX()
has been performed on the cursor or the rowcount of the last operation is not determinable by the interface”. This includesSELECT
statements because we cannot determine the number of rows a query produced until all rows were fetched.
请注意,对于数据库实现的子集,可以在获取 一些 行后更新行计数值。您必须检查您正在连接的特定数据库文档,以查看该实现是否可以执行此操作,或者行数是否必须保持为 -1。当然,您可以随时尝试。
你可以先执行COUNT()
select,或者,如果结果集预计不会太大,使用cursor.fetchall()
,然后使用len()
结果列表。
如果您使用的是微软 sql 服务器,并且您想要获取先前 select 语句中返回的行数,您可以只执行 select @@rowcount
.
例如:
cursor.execute("select @@rowcount")
rowcount = cursor.fetchall()[0][0]