0

我有以下演示代码可以正常工作,但我想捕获 STATISTICS IO 的输出。我找不到捕捉这一点的正确方法。

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]))

我尝试了这段代码,它是从另一个stackoverflow答案中提取的,但没有成功:

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]))

我收到此错误:

c:\test3\MedusaPerfPractice>python MedusaPerfGui.py 提供者=SQLOLEDB;服务器=LINUSG51\SQLEXPRESS;数据库=mytest;集成安全=SSPI;回溯(最后一次调用):文件“MedusaPerfGui.py”,第 38 行,在 myConn.Open() 文件“C:\Users\russ960\AppData\Local\Temp\gen_py\3.5\B691E011-1797-432E-907A -4D8C69339129x0x6x1.py”,第 3358 行,在 Open 中,用户 ID,密码,选项)文件“C:\Program Files\Python35\lib\site-packages\win32com\client__init__.py”,第 459 行,在ApplyTypes self. oleobj .InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args), pywintypes.com_error: (-2147352567, '发生异常。', (0, 'ADODB.Connection', '操作已被用户取消。 ', 'C:\WINDOWS\HELP\ADO270.CHM',

4

0 回答 0