[Tutor] ADO problem

2007-07-16 Thread János Juhász
Dear All,

I have a good sample about using ADO to reach windows active directory.

import win32com.client 
c = win32com.client.Dispatch(ADODB.Connection) 
c.Open(Provider=ADSDSOObject) 

rs,rc=c.Execute( 
SELECT name, description, department
From 'LDAP://DC=VELUX, DC=ORG' 
where objectClass='user' and name='*.ferbau' and department = 'IT'
order by name
) 

while not rs.EOF: 
print rs.Fields[0].Value, rs.Fields[1].Value
rs.MoveNext() 

It print the next result:
IT (u'\xc1kos Szab\xf3',)
IT (u'Szabolcs K\xe1m\xe1n',)
...

So rs.Fields[1] is a tuple.
I tried to turn it to get the first item from this tuple like this

while not rs.EOF: 
print rs.Fields[0].Value, rs.Fields[1][0].Value
rs.MoveNext() 

But it gives the next error 

Traceback (most recent call last):
  File D:\devel\python\admin\AD_ADO.py, line 13, in ?
print rs.Fields[0].Value, rs.Fields[1][0].Value
  File c:\Python24\Lib\site-packages\win32com\client\dynamic.py, line 
228, in
__getitem__
raise TypeError, This object does not support enumeration
TypeError: This object does not support enumeration


How can I print that unicode string?



János Juhász
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ADO problem

2007-07-16 Thread Tim Golden
János Juhász wrote:
 while not rs.EOF: 
 print rs.Fields[0].Value, rs.Fields[1].Value
 rs.MoveNext() 
 
 It print the next result:
 IT (u'\xc1kos Szab\xf3',)
 IT (u'Szabolcs K\xe1m\xe1n',)
 ...
 
 So rs.Fields[1] is a tuple.

Well, here's the most obvious thing:

By the look of it: rs.Fields[1] is *not* a tuple.
It's an instance of some sort. rs.Fields[1].Value
*is* a tuple. So something like this:

rs.Fields[1].Value[0]

should work. I'm not quite clear why that second
field returns a tuple while the first one doesn't.
(Assuming you have reproduced the code and output
faithfully).

To do this specific thing, you might find it easier
to use a module wrapper:

http://tgolden.sc.sabren.com/python/active_directory.html

where your query would become something like (untested):

code
import active_directory

for user in active_directory.search (
   objectClass=User,
   name=*.ferbeau,
   department=IT
):
   print user.name, user.description, user.department

/code

Hope that helps somewhat.

TJG
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] ADO problem

2007-07-16 Thread János Juhász
Hi Tim,

thanks your help.
It is clear for me now.

 From: Tim Golden [EMAIL PROTECTED]
 Subject: Re: [Tutor] ADO problem

 J?nos Juh?sz wrote:
  while not rs.EOF:
  print rs.Fields[0].Value, rs.Fields[1].Value
  rs.MoveNext()
 
  It print the next result:
  IT (u'\xc1kos Szab\xf3',)
  IT (u'Szabolcs K\xe1m\xe1n',)
  ...
 
  So rs.Fields[1] is a tuple.

 Well, here's the most obvious thing:

 By the look of it: rs.Fields[1] is *not* a tuple.
 It's an instance of some sort. rs.Fields[1].Value
 *is* a tuple. So something like this:

 rs.Fields[1].Value[0]

 should work. I'm not quite clear why that second
 field returns a tuple while the first one doesn't.

Yes, It works.

So, I have to use
rs.Fields[1].Value[0] 
instead of
rs.Fields[1][0].Value


 To do this specific thing, you might find it easier
 to use a module wrapper:

 http://tgolden.sc.sabren.com/python/active_directory.html

 where your query would become something like (untested):

Your module works perfectly.
You should know something about the recordsets :)

 code
 import active_directory

 for user in active_directory.search (
 objectClass=User,
 name=*.ferbeau,
 department=IT
 ):
 print user.name, user.description, user.department
 
 /code

Regards,
Janos
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor