我有以下演示代码可以正常工作,但我想捕获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; Traceback(最近一次调用最后一次):文件 " MedusaPerfGui.py",第38行,in myConn.Open()文件" C:\ Users \ russ960 \ AppData \ Local \ Temp \ gen_py \ 3.5 \ B691E011-1797-432E-907A-4D8C69339129x0x6x1.py", 第3358行,在公开赛中 ,UserID,密码,选项)文件" C:\ Program Files \ Python35 \ lib \ site-packages \ win32com \ client__init __。py",line 459,在 ApplyTypes 中 self。 oleobj .InvokeTypes(dispid,0,wFlags,retType,argTypes,* args),pywintypes.com_error:(-2147352567,'发生异常。',(0, ' ADODB.Connection','操作已被用户取消。', ' C:\ WINDOWS \ HELP \ ADO270.CHM',1240661,-2146824576),无)