On 12/19/07, Omer Zak <[EMAIL PROTECTED]> wrote:
> Hello Arik,
> What did you find?
Here's some code I've been using, and as far as I recall produces
Unicode results. Note the wrapper I wrote for returning resulting
records in dictionary form - I believe the results are in unicode.
Let me know how it works for you. The XXX prefixed functions are a
project code prefix I replaced with XXX. It may need an update for
newer versions of MS ADO.
-- Arik
# Microsoft ActiveX Data Objects 2.8 Library
win32com.client.gencache.EnsureModule('{DA9159C1-C9D5-4223-81AC-2B1080701D12}',
0, 1, 0)
def XXXConnect(sSVRHost,sUser,sPass,sDBase):
"Connect to a Menahel database"
sConnStr='Driver={SQL Server};Server=%s;Database=%s;' % (sSVRHost,sDBase)
oConn=win32com.client.Dispatch("ADODB.Connection")
oConn.Properties("Connect Timeout").Value=5
oConn.Open(sConnStr,sUser,sPass)
oConn.Execute('SET DATEFORMAT dmy')
return oConn
def XXXDisconnect(oConn):
oConn.Close()
def XXXListDatabases(sSVRHost,sUser,sPass):
"return the list of database names"
oConn=XXXConnect(sSVRHost,sUser,sPass,'master')
oRecords=OpenRecordSet("SELECT name FROM master.dbo.sysdatabases",oConn)
lNames=[dRecord[u'name'] for dRecord in oRecords]
XXXDisconnect(oConn)
return lNames
def OpenRecordSet(sQuery,oConn):
"creates a generator function that traverses a recordset"
# open a recordset
oRS=win32com.client.Dispatch("ADODB.Recordset")
oRS.CacheSize=1000000
oRS.Open(sQuery,oConn,win32com.client.constants.adOpenForwardOnly,win32com.client.constants.adLockReadOnly)
if oRS.BOF and oRS.EOF:
raise StopIteration
oRS.MoveFirst
# extract the field names
lFields=[f.Name for f in oRS.Fields]
while not oRS.EOF:
lValues=[]
for i in range(len(lFields)):
lValues.append(oRS.Fields(i).Value)
yield dict(zip(lFields,lValues))
oRS.MoveNext()
oRS.Close()
oRS=None
raise StopIteration
_______________________________________________
Python-il mailing list
[email protected]
http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il