环境Windows7+python3.6.4 x64位+AccessDatabaseEngine_X64.exe,执行代码:
import win32com.client
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:/loadrunnerscript/register/database/username20181218.accdb;"
conn.Open(DSN)
在open access数据库时出现如下错误:
Traceback (most recent call last):
File "E:loadrunnerscript
egisterdatabaseaccess.py", line 10, in <module>
conn.Open(DSN)
File "<COMObject ADODB.Connection>", line 3, in Open
File "C:UserslanlianAppDataLocalProgramsPythonPython37-32libsite-packageswin32comclientdynamic.py", line 287, in _ApplyTypes_
result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
pywintypes.com_error: (-2147352567, '发生意外。', (0, 'ADODB.Connection', '未找到提供程序。该程序可能未正确安装。', 'C:\Windows\HELP\ADO270.CHM', 1240655, -2146824582), None)
在更改代码OLEDB版本为12.0版本后依然出现一样的错误。
DSN="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:/loadrunnerscript/register/database/username20181218.accdb;"
之后把python+AccessDatabaseEngine都变成32位后解决问题,执行成功代码如下:
import win32com.client
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:/loadrunnerscript/register/database/username20181218.accdb;"
conn.Open(DSN)