Win32 Com + ADO: How to compare the result of a recordset to 'nothing'

Felix McAllister felix_mcallister at hotmail.com
Tue Sep 16 03:17:20 EDT 2003


Thanks to all who replied to my posting.

There were a number of things wrong with my code.

1. Giles Brown was correct in stating that the NextRecordset method returns a tuple. I should have seen this in the debugger when I printed the value out:
        [Dbg]>>> rs.NextRecordset()
        (<COMObject NextRecordset>, -1)

2. There was a typo in the call to NextRecordset - I had "NextRecordSet" [capital S]. I didn't know that case mattered.

The correct loop code is as follows:
while rs != None:
    rs.MoveFirst()
    while not rs.EOF:
        xmlString = xmlString + rs.Fields[0].Value
        rs.MoveNext()
    rs = rs.NextRecordset()[0]


Felix.
> Hi,
> When using win32com.client, how do you test for a 'nothing' com object as you can in VB? I have an example here when using ADO to loop over multiple recordsets returned from a query. I get the following error:
> 
> Traceback (most recent call last):
>   File "C:\dev\python\MySamples\dbtest.py", line 14, in ?
>     rs.MoveFirst()
>   File "C:\Python23\lib\site-packages\win32com\client\dynamic.py", line 460, in __getattr__
>     raise AttributeError, "%s.%s" % (self._username_, attr)
> AttributeError: <unknown>.MoveFirst
> 
> I'm assuming that after the result of rs.NextRecordSet is invalid somehow (it shouldn't be, BTW, as a similar loop runs fine in VB). 
> 
> In Perl, I'd just use : if (defined($rs)....
> 
> Any help appreciated,
> 
> Felix.
> 
> The code is as follows:
> ###########################3 
> import win32com.client
> conn = win32com.client.Dispatch("ADODB.Connection")
> conn.ConnectionString = "Driver={SQL Server};Server=(local);Database=Test;Trusted_Connection=yes;"
> conn.Open()
> rs = conn.Execute("TestSPXML")[0]
> xmlString = ""
> while rs != None:
>     rs.MoveFirst()    # FAILS HERE ON THE SECOND ITERATION OF THE LOOP
>     while not rs.EOF:
>         xmlString = xmlString + rs.Fields[0].Value
>         rs.MoveNext()
>     rs = rs.NextRecordSet()
> print xmlString
> conn.Close()




More information about the Python-list mailing list