Using ADO in Python

Bill Hunter bkhunter at best.com
Thu May 18 16:27:07 EDT 2000


Oh, cool ... I actually get to answer a question instead of asking
one!  Here's a code fragment that accesses an ADO 2.1 recordset.

# Somewhere toward the front of the script ...
import win32com.client
from win32com.client import constants
from win32com.client import gencache
gencache.EnsureModule('{00000201-0000-0010-8000-00AA006D2EA4}',0,2,1)
# ADO 2.1

# Open ADO connection adoConn
adoConn = win32com.client.Dispatch('ADODB.Connection')
adoConn.ConnectionString = r"DSN=dBASE
Files;DBQ=D:\mydir;DefaultDir=D:\mydir;DriverId=533;FILEDSN=D:\Program
Files\Common Files\ODBC\DataSources\dBASE Files (not
sharable);MaxBufferSize=2048;PageTimeout=5;"
adoConn.ConnectionTimeout = 100
adoConn.Open()

# ... and when you are ready to work with your recordset
# Set up ADO Recordset adoRS and execute some SQL
adoRS=win32com.client.Dispatch('ADODB.Recordset')
someSQL="SELECT foo, bar FROM " + tempTable
adoCmd.CommandText = someSQL
adoCmd.CommandTimeout = 1500
adoCmd.CommandType= constants.adCmdText
adoCmd.ActiveConnection=adoConn
adoRS.CursorType = constants.adOpenKeyset
adoRS.Open(adoCmd)

adoRS.MoveFirst()

for i in range(adoRS.Fields.Count):
    adoField = adoRS.Fields[i]

data = adoRS.GetRows()
nrRec = len(data[0])

On Sat, 13 May 2000 04:01:06 -0700, Bill Tutt <billtut at microsoft.com>
wrote:

>
>
>> From: alien at netspace.net.au [mailto:alien at netspace.net.au]
>> 
>> 
>> I can create an ADODB.Recordset object and use it under Python. No
>> problem. But how do I use a recordset returned, for example, from
>> Connection.Execute()?
>> 
>> When I try something like:
>> 
>> import win32com.client
>> 
>> con = win32com.client.Dispatch("ADODB.Connection")
>> con.Open("Provider=SQLOLEDB.1;Password=password;Persist Security
>> Info=True;User ID=sa;Initial Catalog=Surefire;Data Source=ARMALYTE")
>> rs = win32com.client.Dispatch("ADODB.Recordset")
>> print rs.State
>> rs = con.Execute("SELECT * FROM Stock WHERE Stock_Unique_Id IS NULL")
>> print rs.State
>> 
>> I get the error:
>> 
>> Traceback (innermost last):
>>   File "F:\Code\Python\ado.py", line 8, in ?
>>     print rs.State
>> AttributeError: 'tuple' object has no attribute 'State'
>> 
>> The first "print rs.State" works, the second does not. 
>> 
>> Any help would be appreciated.
>> 
>
>Execute is returning a tuple.
>Replace the 2nd "print rs.State" with "print rs" and you'll see whats going
>on.
>The reason that Execute is returning a tuple is because Execute has an out
>parameter besides the normal recordset return value. 
>
>Something like rs = con.Execute(....)[0] might do the trick.
>
>Bill




More information about the Python-list mailing list