mkleehammer / pyodbc

Python ODBC bridge
https://github.com/mkleehammer/pyodbc/wiki
MIT No Attribution
2.88k stars 562 forks source link

Unsure how to retrieve results from pyodbc.Cursor.statistics() #1105

Closed tcdejong closed 1 year ago

tcdejong commented 1 year ago

Environment

Issue

The documentation for pyodbc.Cursor describes the Cursor.statistics method. However, return type is another Cursor. I'm not sure how to access the results, and there's no example in the documentation.

# c is a connected Cursor
# c.tables() works as expected
tables = c.tables()
print(len(tables)) # -> 778
print(type(tables[0])) # As expected: -> pyodbc.Row

table_name = tables[0][2]
print(table_name) # -> real_table_name

c.statistics(table_name) # -> pyodbc.Cursor

How do I access the results from .statistics?

gordthompson commented 1 year ago

Just call .fetchall():


>>> crsr.execute("CREATE TABLE foo (id int primary key, txt nvarchar(50))")
<pyodbc.Cursor object at 0x0000001498FCBD30>
>>> st = crsr.statistics("foo")
>>> st
<pyodbc.Cursor object at 0x0000001498FCBD30>
>>> st.fetchall()
[('test', 'dbo', 'foo', None, None, None, 0, None, None, None, 0, 0, None), ('test', 'dbo', 'foo', 0, 'foo', 'PK__foo__3213E83F52719F64', 1, 1, 'id', 'A', 0, 0, None)]