无法使用 python adodbapi 库建立数据库连接

Unable to make database connection using python adodbapi library

我正在尝试使用 python 库 adodbapilatest OLEDB 提供程序建立 SQL 服务器数据库连接,使用以下代码-

import adodbapi
import configparser

def try_connection():    
    _SERVER_NAME = "SERVERNAME"
    _DATABASE = "TESTDB"
    _USERNAME = "TERSTUSER"
    _PASSWORD = "TESTPWD"  

    conn = adodbapi.connect("PROVIDER=MSOLEDBSQL;Data Source={0};Database={1};UID={2};PWD={3};".format(_SERVER_NAME,_DATABASE,_USERNAME,_PASSWORD))
    cursor = conn.cursor()

print( try_connection() )

但是,我无法建立连接。我遇到了无法弄清楚的巨大跟踪错误:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\adodbapi\adodbapi.py", line 97, in make_COM_connecter
    c = Dispatch('ADODB.Connection') #connect _after_ CoIninialize v2.1.1 adamvan
NameError: name 'Dispatch' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\adodbapi\adodbapi.py", line 116, in connect
    co.connect(kwargs)
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\adodbapi\adodbapi.py", line 266, in connect
    self.connector = connection_maker()
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\adodbapi\adodbapi.py", line 99, in make_COM_connecter
    raise api.InterfaceError ("Windows COM Error: Dispatch('ADODB.Connection') failed.")
adodbapi.apibase.InterfaceError: Windows COM Error: Dispatch('ADODB.Connection') failed.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "TestAdodb.py", line 41, in <module>
    print( try_connection() )
  File "TestAdodb.py", line 18, in try_connection
    conn = adodbapi.connect("PROVIDER=MSOLEDBSQL;Data Source={0};Database={1};UID={2};PWD={3};".format(_SERVER_NAME,_DATABASE,_USERNAME,_PASSWORD))
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\adodbapi\adodbapi.py", line 120, in connect
    raise api.OperationalError(e, message)
adodbapi.apibase.OperationalError: (InterfaceError("Windows COM Error: Dispatch('ADODB.Connection') failed.",), 'Error opening connection to "PROVIDER=MSOLEDBSQL;Data Source=SERVERNAME;Database=TESTDB;UID=TERSTUSER;PWD=TESTPWD;"')

谁能告诉我连接字符串中的 wrong/missing 是什么?

引用自pypi,

Prerequisites:

  • C Python 2.7 or 3.5 or higher and pywin32 (Mark Hammond's python for windows extensions.) or Iron Python 2.7 or higher. (works in IPy2.0 for all data types except BUFFER)

Installation:

  • (C-Python on Windows): Install pywin32 ("pip install pywin32") which includes adodbapi.
  • (IronPython on Windows): Download adodbapi from http://sf.net/projects/adodbapi. Unpack the zip. Open a command window as an administrator. CD to the folder containing the unzipped files. Run "setup.py install" using the IronPython of your choice.

您是否安装了 adodbapi 库的所有先决条件?

如果我没有安装 pywin32 库,我能够重现错误。

使用以下命令安装 pywin32 库:

pip install pywin32