首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >查询桌面搜索时如何使用win32com处理溢出?

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

Stack Overflow用户
提问于 2018-06-09 09:37:12
回答 1查看 640关注 0票数 5

我正在使用Python + ADO查询Windows桌面搜索JET (ESE)数据库。它是有效的,但是在使用MoveNext前进到下一条记录时,在大约7600条记录之后,我得到了一个异常。我知道它不是在EOF,因为我可以在VBScript中运行相同的查询,并通过相同的查询获得更多的记录。

异常回溯

代码语言:javascript
复制
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显示它是:

  • Constant: field
  • Value: 3721 -2146824567 0x800A0E89
  • Description:数据值太大,无法由adErrDataOverflow数据类型表示。

这在VBScript中工作得很好(但可能只是因为错误处理不好)。PowerShell有以下错误(在获取比Python更远的内容之后,大约与VBScript获取的位置相同):

代码语言:javascript
复制
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

代码语言:javascript
复制
#!/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"),这使得它比Python语言中的其他尝试多一倍,但最终在UnicodeEncodeError上失败(这似乎与上面显示的其他错误无关,
  • 我尝试使用PowerShell,但也收到了COMException (错误输出如上所示)。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-10 02:02:32

您的trackback显示-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)

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

代码语言:javascript
复制
conn.CommandTimeout = 0
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50770172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档