gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:16:28 26 4
gpt4 key购买 nike

我正在使用 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 表明它是:

  • 常量: 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(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

到目前为止我尝试了什么

  • 我尝试从我的查询中删除 "System.Message.AttachmentNames" 列/字段,但这实际上使它失败得更快/在出现相同错误的记录更少(第一个数字在异常(exception)情况下 args 是相同的)。
  • 我只尝试使用一个字段 ("System.ItemName"),这使得它比 Python 中的其他尝试多了一倍,但最终因 UnicodeEncodeError(似乎不相关)而失败到上面显示的其他错误,这会阻止它进入带有该错误的文件名)。
  • 我尝试使用 PowerShell 并收到了 COMException(如上所示的错误输出)。

最佳答案

您的引用通告显示 -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

关于python - 如何使用win32com处理查询桌面搜索时溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50770172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com