0

I have the following demonstration code that works just fine but I want to capture the output of STATISTICS IO. I cannot find the proper way to capture this.

import adodbapi

server = 'MYCOMPUTER\SQLEXPRESS' 
database = 'mytest' 
connString = 'Provider=SQLOLEDB;Server=' + server + ';Database=' + database + ';Integrated Security=SSPI;'
print(connString)
myConn = adodbapi.connect(connString)
cursor = myConn.cursor()
cursor.execute('SET STATISTICS IO ON; SELECT * FROM dbo.mytab')
results = cursor.fetchall()
for i in results:
    print("First value: {} and Second value: {}".format(i[0],i[1]))

I tried this code which is a is pulled from a another stackoverflow answer with no success:

import adodbapi
import win32com
import pythoncom
##from win32com.client import gencache
##gencache.EnsureModule('{2A75196C-D9EB-4129-B803-931327F72D5C}', 0, 2, 8)

defaultNamedOptArg=pythoncom.Empty
defaultNamedNotOptArg=pythoncom.Empty
defaultUnnamedArg=pythoncom.Empty

class events():
    def OnInfoMessage(self, pError, adStatus, pConnection):
        print('Info Message')
        a = pError.QueryInterface(pythoncom.IID_IDispatch)
        a = win32com.client.Dispatch(a)
        print(a.Description)
        print(a.Number)
        print(a.Source)
        #print 'B', adStatus
        c = pConnection.QueryInterface(pythoncom.IID_IDispatch)
        c = win32com.client.Dispatch(c)
        print(c.Errors.Count)
        print(c.Errors.Item(0).Description)
        print(c.Errors.Clear())
        print('c', adStatus)

if __name__ == '__main__':        
    server = 'mybox\SQLEXPRESS' 
    database = 'mytest' 
    username = 'testusr' 
    password = 'password' 
    connString = 'Provider=SQLOLEDB;Server=' + server + ';Database=' + database + ';Integrated Security=SSPI;'
    print(connString)

    myConn = win32com.client.DispatchWithEvents("ADODB.Connection", events)
    myConn.ConnectionString = connString
    myConn.Open()
    con = adodbapi.cursor(myConn)
    cursor = con.cursor()
    cursor.execute('SET STATISTICS IO ON; SELECT * FROM dbo.mytab')
    results = cursor.fetchall()
    for i in results:
        print("First value: {} and Second value: {}".format(i[0],i[1]))

I get this error:

c:\test3\MedusaPerfPractice>python MedusaPerfGui.py Provider=SQLOLEDB;Server=LINUSG51\SQLEXPRESS;Database=mytest;Integrated Security=SSPI; Traceback (most recent call last): File "MedusaPerfGui.py", line 38, in myConn.Open() File "C:\Users\russ960\AppData\Local\Temp\gen_py\3.5\B691E011-1797-432E-907A-4D8C69339129x0x6x1.py", line 3358, in Open , UserID, Password, Options) File "C:\Program Files\Python35\lib\site-packages\win32com\client__init__.py", line 459, in ApplyTypes self.oleobj.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args), pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'ADODB.Connection', 'Operation has been cancelled by the user.', 'C:\WINDOWS\HELP\ADO270.CHM', 1240661, -2146824576), None)

Russ960
  • 1,109
  • 2
  • 17
  • 33
  • In C# there is [InfoMessage](http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.infomessage.aspx), maybe you can find something like this for Python. – gofr1 Oct 23 '17 at 08:55
  • I used one example I found but it does not work properly. I'm adding to the question in case this helps – Russ960 Oct 31 '17 at 03:29

0 Answers0