John Hass John Hass - 22 days ago 8
Python Question

pymssql can connect but does not return any results

I am connecting to an MS SQL server with pymssql. I can connect by

tsql -H ip -p 1433 -U xx - p xx
and by jupyter notebook. The connection does not return any errors.

However, I tried many queries with pymssql but none of them returned results.

For example,
cursor.execute('SELECT * FROM INFORMATION_SCHEMA.TABLES ')


What should I check now?

Answer

As mentioned in the "Iterating through results" section of the pymssql examples, you can simply iterate through the rows of the result set like this:

crsr = conn.cursor()
crsr.execute("SELECT * FROM INFORMATION_SCHEMA.TABLES")
for row in crsr:
    print(row)

Or, to use a more standard DB-API approach:

crsr = conn.cursor()
crsr.execute("SELECT * FROM INFORMATION_SCHEMA.TABLES")
for row in crsr.fetchall():
    print(row)
Comments