我正在使用 Python + ADO 查询 Windows 桌面搜索 JET (ESE) 数据库。它可以工作,但是在 ~7600 条记录之后,使用MoveNext
. 我知道它不在 EOF,因为我可以在 VBScript 中运行相同的查询并使用相同的查询获得更多记录。
异常回溯
Traceback (most recent call last):
File "test_desktop_search.py", line 60, in <module>
record_set.MoveNext()
File "<COMObject ADODB.Recordset>", line 2, in MoveNext
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147215865), None)
查询该错误表明它是:
- 常量: adErrDataOverflow
- 值: 3721 -2146824567 0x800A0E89
- 说明:数据值太大,字段数据类型无法表示。
这在 VBScript 中运行良好(但可能只是由于错误处理不当)。PowerShell 有以下错误(在比 Python 走得更远之后,与 VBScript 得到的位置差不多):
Exception from HRESULT: 0x80041607
At C:\Users\doday\PycharmProjects\desktop_search_test\Get-DesktopSearchData.ps1:43 char:5
+ $recordSet.MoveNext();
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
我在 Microsoft 文档中找不到此错误代码,但它可能是相关的。如您所见,Facility 字段为 4(特定于接口的 HRESULT 错误),代码为 1607。
MCVE
#!/usr/bin/env python
"""
Test querying Desktop Search from Python
"""
import csv
import pywintypes
from win32com.client import Dispatch
# connection
conn = Dispatch("ADODB.Connection")
connstr = "Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"
conn.Open(connstr)
# record set
record_set = Dispatch("ADODB.Recordset")
q = "SELECT System.ItemName, System.ItemTypeText, System.Size, System.IsDeleted, System.DateAccessed, System.Kind, System.ItemDate, System.Search.Store, System.ItemParticipants, System.ItemAuthors, System.IsRead, System.Message.AttachmentNames FROM SystemIndex"
# record_set.ActiveConnection = conn
record_set.Open(q, conn)
# header
# I'm only selecting a few fields for this test, see
# https://msdn.microsoft.com/en-us/library/windows/desktop/bb419046(v=vs.85).aspx
header = [
"System.ItemName",
"System.ItemTypeText",
"System.Size",
"System.IsDeleted",
"System.DateAccessed",
"System.Kind",
"System.ItemDate",
"System.Search.Store",
"System.ItemParticipants",
"System.ItemAuthors",
"System.IsRead",
"System.Message.AttachmentNames"
]
# output to file
with open("ds_output.tsv", "w", newline='') as out_f:
w = csv.DictWriter(out_f, fieldnames=header, delimiter='\t')
w.writeheader()
record_set.MoveFirst()
while not record_set.EOF:
record = dict.fromkeys(header)
# populate fields
for h in header:
record[h] = record_set.Fields.Item(h).Value
# write record
w.writerow(record)
try:
record_set.MoveNext()
except pywintypes.com_error as e:
# can't figure out how to resolve this or at least advance to next record despite this error
print("Error: {}".format(e.args))
# clean up
record_set.Close()
record_set = None
conn.Close()
conn = None
到目前为止我尝试过的
- 我尝试
"System.Message.AttachmentNames"
从我的查询中删除列/字段,但这实际上使它失败得更快/在更少的记录之后出现了相同的错误(异常中的第一个数字args
是相同的)。 "System.ItemName"
我只尝试使用一个字段(带有该错误的文件名)。- 我尝试使用 PowerShell 并且还收到了一个
COMException
(如上所示的错误输出)。