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

logistix at cathoderaymission.net logistix at cathoderaymission.net
Mon Sep 15 15:08:39 EDT 2003


"Felix McAllister" <felix_mcallister at hotmail.com> wrote in message news:<tvj9b.31786$pK2.53500 at news.indigo.ie>...
> 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()

Recordsets have a .BOF property that is similar to .EOF, but indicates
that your cursor is before the first record.  If both .BOF and .EOF
are true, you have a null recordset.  So something like:

if not(rst.BOF and rst.EOF):
    rst.MoveFirst()

should work.




More information about the Python-list mailing list