如何使用win32com处理查询桌面搜索时溢出?

How to use win32com to handle overflow when querying Desktop Search?

我正在使用 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)

Querying that error表明是:

这在 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(interface-specific HRESULT error),代码为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

到目前为止我尝试了什么

您的引用通告显示了 -2147352567 错误代码。这是 DISP_E_EXCEPTION,一个非常通用的 COM 异常。 它包装了一个-2147215865错误,也就是0x80041607,也就是QUERY_E_TIMEDOUT.

我使用这个免费的网站工具——我自己制作的——来查找错误和其他类似的常量:https://www.magnumdb.com/search?q=-2147215865

所以,事实上,它们都报告相同的超时错误。

您可以按照此处所述增加 ADO 超时:Having Problems With SystemIndex (I Love It But Can't Make It Work How I Want)

或者您可以完全删除它,使用如下代码:

conn.CommandTimeout = 0