How many rows are in my _mssql.MSSQLConnection object ?

46 views
Skip to first unread message

Patrick Rynhart

unread,
Mar 3, 2019, 10:59:56 PM3/3/19
to pymssql
I'm trying to work out how many rows are being returned by an _mssql.MSSQLConnection object.  (Aside there is a method "rows_affected", but I'm just using a SELECT statement so nothing is changing).

Is there a better way that this hack ?

matches = 0
query = "SELECT TOP 100 a.asset_id, .........."
conn.execute_query(query)

for row in conn:
    matches += 1
    # do operations on rows

# matches contains number of rows.

....

I tried len(conn) etc but get

TypeError: object of type '_mssql.MSSQLConnection' has no len()

Thanks in Advance,

Patrick

Alex Hagerman

unread,
Apr 2, 2019, 11:39:47 AM4/2/19
to pymssql
Are you trying to see how many rows are loaded in at once? I'm asking because your query has a TOP100, and your matches would tell you total rows, but maybe not how many rows are in the cursor at once. Also this has the behavior of an iterator, so I'd have to look at the underlying implementation but it would either be how many return to the cursor, or if it's a generator then it would be doing a next/yield pattern.
Reply all
Reply to author
Forward
0 new messages