ADODBAPI No. Of Open Connection with database

感情迁移 提交于 2019-12-02 07:58:53

问题


I want to get the count of no. of connections currently open with an ms-access database.

For example two applications are working with the same database. Then how can I get this count? Is there is ms-access function or any facility in pypyodbc?

Using adodbapi, how can I get no. of open connections with a database??

I tried the following code.

#importing adodbapi 
import adodbapi # success 
#connection to database using the DSN 'test'
myConn = adodbapi.connect('test') # success
#get no. of open connection using openschema 
myConn.connector.OpenSchema(-1, None,"{947bb102-5d43-11d1-bdbf-00c04fb92675}") #fail

It gives the following error.

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'ADODB.Connection', u'Object or provider is not capable of
performing requested operation.', u'C:\WINDOWS\HELP\ADO270.CHM',
1240648, -2146825037), None)

Can anybody provide solution?


回答1:


Personally I would be inclined to avoid fussing with adodbapi in this case and just have my Python script write a little VBScript to create a tab-separated list of machines with open connections, run the VBScript via subprocess.Popen, and parse the results:

import os
import subprocess

## test data
databaseFileSpec = r"Z:\pyTest.mdb"

vbsFileSpec =  os.environ['TEMP'] + r"\mypytemp.vbs"

scriptCode = """Option Explicit
Dim con, rst, strOut, strSeparator
Const adSchemaProviderSpecific = -1
Set con = CreateObject("ADODB.Connection")
con.Open( _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source="""
scriptCode += databaseFileSpec
scriptCode += """")
Set rst = con.OpenSchema( _
        adSchemaProviderSpecific, _
        , _
        "{947bb102-5d43-11d1-bdbf-00c04fb92675}")
strOut = ""
strSeparator = ""
Do While Not rst.EOF
    If rst(2).Value = "True" Then
        strOut = strOut & strSeparator & Left(rst(0).Value, Len(Trim(rst(0).Value)) - 1)
        strSeparator = vbTab
    End If
    rst.MoveNext
Loop
WScript.Echo strOut
rst.Close
con.Close"""

f = open(vbsFileSpec, 'w')
f.write(scriptCode)
f.close()

tabString = subprocess.Popen(
    "cscript /nologo \"" + vbsFileSpec + "\"",
    shell=True,
    stdout=subprocess.PIPE).stdout.read()
os.remove(vbsFileSpec)

print 'The following machines are connected to "' + databaseFileSpec + '":'
for x in tabString.split("\t"):
    print x

When I have the database open on two different machines and run the above script I get

The following machines are connected to "Z:\pyTest.mdb":
TESTPC
GORD01
GORD01

My notebook (GORD01) shows up twice because I have the database open in Access and the VBScript also has a connection open while it is running.



来源:https://stackoverflow.com/questions/24796106/adodbapi-no-of-open-connection-with-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!