Discussion:
[python-win32] driving MS Access from python
Phill Atwood
2006-03-12 22:19:01 UTC
Permalink
Need to programatically reproduce some activities a user does in MS Access.
Need to open a mdb file. Run some queries. Look at some tables.

So far

import win32com.client
a = win32com.client.Dispatch("Access.Application")
a.Visible = 1
db = a.OpenCurrentDatabase(filename)

Which seems to work so far. But now I need to run 2 of 3 named
queries. Then switch
to the Tables view and collect the info that has now populated the
tables and extract them
into python code. I having a hard time scaring up appropriate docs on
the MS Access
win32com API for this. I've been looking for VBA style docs or anything
but I'm still so
far just sniffing the corners...

Any help is appreciated.

Phill
Tim Golden
2006-03-13 16:45:19 UTC
Permalink
[Phill Atwood]

|
| Need to programatically reproduce some activities a user does
| in MS Access.
| Need to open a mdb file. Run some queries. Look at some tables.

[...]

| I having a hard time scaring up appropriate docs on
| the MS Access win32com API for this. I've been
| looking for VBA style docs or anything but I'm still so
| far just sniffing the corners...

I don't use Access myself, bu do I take it you've already seen this?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11
/html/actocOMMap_HV01049589.asp

My usual first port of call is to google for

site:msdn.microsoft.com <whatever> object model

Often Googling for Delphi examples works quite well as
they are in a similar position to Pythoneers (they like
their language/environment, but want to do things with
MS software) but have been around longer.

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________
e***@srs.gov
2006-03-13 17:04:35 UTC
Permalink
I would suggest using ADO (Microsoft Active X Data Objects) for this. Much
cleaner (and more flexible) than trying to use the Access executable.

import win32com.client, string

#Establish the ADO DB Connection
class fields:
def __init__(self, dbpath, tblName):
self.dbpath = dbpath
self.tblName=tblName
def add(self, sqlstatement):
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=' +
self.dbpath + ';'
conn.Open(DSN)

#Query the recordset - should be in module with establishing
connection stuff
rs = win32com.client.Dispatch(r'ADODB.Recordset')
print sqlstatement
try:
rs.Open(sqlstatement, conn,1 ,3)
except:
print 'DB Error'

conn.Close()
#Update 1 record in the database. Fieldlist is a list of tuples
consisting of (Name, Value)
def update(self, attdata, fieldlist, wherecl):
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=' +
self.dbpath + ';'
conn.Open(DSN)

rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs_name = 'UPDATE ' + self.tblName + ' SET '

listlen = len(attdata)
print 'List is ' + str(listlen) + " items long"
for index in range (0,listlen):
field = attdata[index]
ftype = fieldlist[index][1]
#Add function to de-capitalize string,
if string.capitalize(ftype[0:7]) == 'Varchar':
try:
data = '"' + str(field[1]) + '"'
except:
data = '"' + field[1] + '"'
elif string.capitalize(ftype[0:4]) == "Date":
try:
data = '#' + str(field[1]) + '#'
except:
data = '#' + field[1] + '#'
else:
data = field[1]

if index == 0:
rs_name = rs_name + self.tblName+ '.' + field[0] + ' = ' +
data
else:
try:
rs_name = rs_name + ', ' + self.tblName+ '.' +
field[0] + ' = ' + data
except:
print 'Error in data type match, debug data follows: '
print rs_name
print self.tblName
print field[0]
print data
rs_name = rs_name + ' ' + wherecl + ';'
rs.Open(rs_name, conn,1 ,3)
conn.Close()

This script:

1) Opens a database conenction
2) Allows execution of an append query (supplied as a SQL string)
3) Allows exceution of an update query.

Other sources on info:

MSDN query on ADO

There is an ADO / Python page out there (check google) but I can't
remember the URL.

HTH,

Eric

Eric B. Powell
BSRI
Electronic Aids
(803)208-6207




Phill Atwood <***@phillatwood.name>
Sent by: python-win32-***@python.org
03/12/2006 05:19 PM

To
python-***@python.org
cc

Subject
[python-win32] driving MS Access from python








Need to programatically reproduce some activities a user does in MS
Access.
Need to open a mdb file. Run some queries. Look at some tables.

So far

import win32com.client
a = win32com.client.Dispatch("Access.Application")
a.Visible = 1
db = a.OpenCurrentDatabase(filename)

Which seems to work so far. But now I need to run 2 of 3 named
queries. Then switch
to the Tables view and collect the info that has now populated the
tables and extract them
into python code. I having a hard time scaring up appropriate docs on
the MS Access
win32com API for this. I've been looking for VBA style docs or anything
but I'm still so
far just sniffing the corners...

Any help is appreciated.

Phill
Phill Atwood
2006-03-13 20:28:58 UTC
Permalink
Eric,

Thanks. This seems like a good idea. I noticed also ADOdb for Python.
Would this work do you know? Since I'm doing win/ms access I think I'll
need to use mxodbc but I don't know how to install that on Windows.

In your example below it is not clear (to me) how to get the dimensions
of the recordset and navigate thru (eg. print) all the resultant
fields. Is there something similar to the ADOdb for
Python
while not cursor.EOF:
print cursor.fields
cursor.MoveNext()
cursor.close()
Post by e***@srs.gov
I would suggest using ADO (Microsoft Active X Data Objects) for this.
Much cleaner (and more flexible) than trying to use the Access
executable.
import win32com.client, string
#Establish the ADO DB Connection
self.dbpath = dbpath
self.tblName=tblName
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=' +
self.dbpath + ';'
conn.Open(DSN)
#Query the recordset - should be in module with establishing
connection stuff
rs = win32com.client.Dispatch(r'ADODB.Recordset')
print sqlstatement
rs.Open(sqlstatement, conn,1 ,3)
print 'DB Error'
conn.Close()
#Update 1 record in the database. Fieldlist is a list of tuples
consisting of (Name, Value)
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=' +
self.dbpath + ';'
conn.Open(DSN)
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs_name = 'UPDATE ' + self.tblName + ' SET '
listlen = len(attdata)
print 'List is ' + str(listlen) + " items long"
field = attdata[index]
ftype = fieldlist[index][1]
#Add function to de-capitalize string,
data = '"' + str(field[1]) + '"'
data = '"' + field[1] + '"'
data = '#' + str(field[1]) + '#'
data = '#' + field[1] + '#'
data = field[1]
rs_name = rs_name + self.tblName+ '.' + field[0] + ' =
' + data
rs_name = rs_name + ', ' + self.tblName+ '.' +
field[0] + ' = ' + data
print 'Error in data type match, debug data follows: '
print rs_name
print self.tblName
print field[0]
print data
rs_name = rs_name + ' ' + wherecl + ';'
rs.Open(rs_name, conn,1 ,3)
conn.Close()
1) Opens a database conenction
2) Allows execution of an append query (supplied as a SQL string)
3) Allows exceution of an update query.
MSDN query on ADO
There is an ADO / Python page out there (check google) but I can't
remember the URL.
HTH,
Eric
Eric B. Powell
BSRI
Electronic Aids
(803)208-6207
03/12/2006 05:19 PM
To
cc
Subject
[python-win32] driving MS Access from python
Need to programatically reproduce some activities a user does in MS
Access.
Need to open a mdb file. Run some queries. Look at some tables.
So far
import win32com.client
a = win32com.client.Dispatch("Access.Application")
a.Visible = 1
db = a.OpenCurrentDatabase(filename)
Which seems to work so far. But now I need to run 2 of 3 named
queries. Then switch
to the Tables view and collect the info that has now populated the
tables and extract them
into python code. I having a hard time scaring up appropriate docs on
the MS Access
win32com API for this. I've been looking for VBA style docs or anything
but I'm still so
far just sniffing the corners...
Any help is appreciated.
Phill
_______________________________________________
Python-win32 mailing list
http://mail.python.org/mailman/listinfo/python-win32
Phill Atwood
2006-03-13 21:47:09 UTC
Permalink
I'm trying to get at some data in MS Access. This long query updates a
table. I'm wondering if this query is failing. Anyways, when I run


dbpath = os.getcwd() + "\\Test (2).mdb"
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=' + dbpath + ';'
conn.Open(DSN)
rs = win32com.client.Dispatch(r'ADODB.Recordset')
sqlstatement = 'UPDATE [Acute avian (liquids)] INNER JOIN
(([Label info] INNER JOIN [User selection] ON [Label info].Crop = [User
selection].[Select Crop]) INNER JOIN [Acute mammal (liquids)] ON [Label
info].[AI Code] = [Acute mammal (liquids)].[AI Code]) ON ([Acute avian
(liquids)].[AI Code] = [Acute mammal (liquids)].[AI Code]) AND ([Acute
avian (liquids)].Group = [Acute mammal (liquids)].Group) SET [Acute
avian (liquids)].[Application rate -kg per ha] = [Label info]![AI rate -
kg per ha], [Acute mammal (liquids)].[Application rate -kg per ha] =
[Label info]![AI rate - kg per ha] WHERE ((([Label info].[Application
detail])="Ground spray"));'
print sqlstatement
rs.Open(sqlstatement, conn)
print "rs.RecordCount = " + rs.RecordCount


I get the error

[snip...]
intypes.com_error: (-2147352567, 'Exception occurred.', (0,
'ADODB.Recordset', 'Operation is not allowed when the object is
closed.', 'C:\\WINDOWS\\HELP\\ADO270.CHM', 1240653, -2146824584), None)

on the very last statement.

I'm still new to this stuff and trying to get my head around it. I need
to be able to duplicate this
query which works fine by hand in Access. Any help is appreciated.

Thanks,
Phill
e***@srs.gov
2006-03-13 20:42:51 UTC
Permalink
Exactly.

Once you set up the iDispatch to point to the ADODB object library, you
can then use the ADO methods and objects.

One advantage of ADO is that it SHOULD be installed as part of Windows (I
think...I have never run into a case where is wasn't, anyone on the list
want to correct me on this????), so things should work out of the box (so
to speak).

To get the dimensions of the recordset you could use the Fields and Rows
count count methods in ADO

Open Connection stuff...
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(sqlstatement, conn,1 ,3)
FC = rs.Fields.Count
RC = rs.Rows.Count

Further for navigating between rows, you have:

rs.MoveFirst
rs.MoveLast
rs.MoveNext
rs.MovePrevious ???? (I think you can iterate backward).

To get the value of a given field, descend through the
Recordset->Fields->Item() chain as follows:

val = rs.Fields.Item(1).Value

Where the iterator in item can be either an ineteger or a fieldname:

val = rs.Fields.Item("Fred").Value


ADO can also access/fire an existing action query or return the records
from a stored query.

More info:

http://www.w3schools.com/ado/default.asp
http://www.mayukhbose.com/python/ado/ado-python.php

HTH,

Eric

Eric B. Powell
BSRI
Electronic Aids
(803)208-6207




Phill Atwood <***@phillatwood.name>
03/13/2006 03:28 PM

To
***@srs.gov
cc
python-***@python.org
Subject
Re: [python-win32] driving MS Access from python







Eric,

Thanks. This seems like a good idea. I noticed also ADOdb for Python.
Would this work do you know? Since I'm doing win/ms access I think I'll
need to use mxodbc but I don't know how to install that on Windows.

In your example below it is not clear (to me) how to get the dimensions
of the recordset and navigate thru (eg. print) all the resultant
fields. Is there something similar to the ADOdb for
Python
while not cursor.EOF:
print cursor.fields
cursor.MoveNext()
cursor.close()
Post by e***@srs.gov
I would suggest using ADO (Microsoft Active X Data Objects) for this.
Much cleaner (and more flexible) than trying to use the Access
executable.
import win32com.client, string
#Establish the ADO DB Connection
self.dbpath = dbpath
self.tblName=tblName
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=' +
self.dbpath + ';'
conn.Open(DSN)
#Query the recordset - should be in module with establishing
connection stuff
rs = win32com.client.Dispatch(r'ADODB.Recordset')
print sqlstatement
rs.Open(sqlstatement, conn,1 ,3)
print 'DB Error'
conn.Close()
#Update 1 record in the database. Fieldlist is a list of tuples
consisting of (Name, Value)
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=' +
self.dbpath + ';'
conn.Open(DSN)
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs_name = 'UPDATE ' + self.tblName + ' SET '
listlen = len(attdata)
print 'List is ' + str(listlen) + " items long"
field = attdata[index]
ftype = fieldlist[index][1]
#Add function to de-capitalize string,
data = '"' + str(field[1]) + '"'
data = '"' + field[1] + '"'
data = '#' + str(field[1]) + '#'
data = '#' + field[1] + '#'
data = field[1]
rs_name = rs_name + self.tblName+ '.' + field[0] + ' =
' + data
rs_name = rs_name + ', ' + self.tblName+ '.' +
field[0] + ' = ' + data
print 'Error in data type match, debug data
follows: '
print rs_name
print self.tblName
print field[0]
print data
rs_name = rs_name + ' ' + wherecl + ';'
rs.Open(rs_name, conn,1 ,3)
conn.Close()
1) Opens a database conenction
2) Allows execution of an append query (supplied as a SQL string)
3) Allows exceution of an update query.
MSDN query on ADO
There is an ADO / Python page out there (check google) but I can't
remember the URL.
HTH,
Eric
Eric B. Powell
BSRI
Electronic Aids
(803)208-6207
03/12/2006 05:19 PM
To
cc
Subject
[python-win32] driving MS Access from python
Need to programatically reproduce some activities a user does in MS
Access.
Need to open a mdb file. Run some queries. Look at some tables.
So far
import win32com.client
a = win32com.client.Dispatch("Access.Application")
a.Visible = 1
db = a.OpenCurrentDatabase(filename)
Which seems to work so far. But now I need to run 2 of 3 named
queries. Then switch
to the Tables view and collect the info that has now populated the
tables and extract them
into python code. I having a hard time scaring up appropriate docs on
the MS Access
win32com API for this. I've been looking for VBA style docs or anything
but I'm still so
far just sniffing the corners...
Any help is appreciated.
Phill
_______________________________________________
Python-win32 mailing list
http://mail.python.org/mailman/listinfo/python-win32
Robert Brewer
2006-03-13 22:02:10 UTC
Permalink
Post by Phill Atwood
intypes.com_error: (-2147352567, 'Exception occurred.', (0,
'ADODB.Recordset', 'Operation is not allowed when the object is
closed.', 'C:\\WINDOWS\\HELP\\ADO270.CHM', 1240653,
-2146824584), None)
IIRC, you need to set Cursorlocation before calling Recordcount. Google
for more info.

adUseClient = 3
rs.Cursorlocation = adUseClient



Robert Brewer
System Architect
Amor Ministries
***@amor.org
Tim Roberts
2006-03-13 22:38:22 UTC
Permalink
Post by Phill Atwood
Thanks. This seems like a good idea. I noticed also ADOdb for Python.
Would this work do you know? Since I'm doing win/ms access I think I'll
need to use mxodbc but I don't know how to install that on Windows.
In your example below it is not clear (to me) how to get the dimensions
of the recordset and navigate thru (eg. print) all the resultant
fields. Is there something similar to the ADOdb for
Python
One of the issues with Access is that there are an "embarrassment of
riches" of methods to get to an Access database on Windows. You have
ODBC directly, mxodbc, Jet, DAO, Jet via ADODB, ODBC via ADODB, DAO via
ADODB.

ADODB is a very good way of doing it. One of the advantages is that the
same code works for Postgres or MySQL; you just change the connection
string (although that's mostly true for ODBC, too). Plus, by creating
an ADOX.Catalog object, you can stroll through the metadata as well.
You can fetch the list of tables, and the fields within the tables, and
so on.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Phill Atwood
2006-03-13 23:04:53 UTC
Permalink
Tim,

Thanks. I've downloaded and installed adodb for Python. But I guess I
need to install mxODBC as well. But I'm not quite understanding the
docs I'm reading on how to do this. It seems very complex....

Phill
Post by Tim Roberts
Post by Phill Atwood
Thanks. This seems like a good idea. I noticed also ADOdb for Python.
Would this work do you know? Since I'm doing win/ms access I think I'll
need to use mxodbc but I don't know how to install that on Windows.
In your example below it is not clear (to me) how to get the dimensions
of the recordset and navigate thru (eg. print) all the resultant
fields. Is there something similar to the ADOdb for
Python
One of the issues with Access is that there are an "embarrassment of
riches" of methods to get to an Access database on Windows. You have
ODBC directly, mxodbc, Jet, DAO, Jet via ADODB, ODBC via ADODB, DAO via
ADODB.
ADODB is a very good way of doing it. One of the advantages is that the
same code works for Postgres or MySQL; you just change the connection
string (although that's mostly true for ODBC, too). Plus, by creating
an ADOX.Catalog object, you can stroll through the metadata as well.
You can fetch the list of tables, and the fields within the tables, and
so on.
John Machin
2006-03-13 23:19:46 UTC
Permalink
Post by Phill Atwood
Tim,
Thanks. I've downloaded and installed adodb for Python. But I guess I
need to install mxODBC as well. But I'm not quite understanding the
docs I'm reading on how to do this. It seems very complex....
Phill
Phill,

Sorry, you've lost me ... I thought "this" (installing mxODBC) was as
simple as downloading the two installers ("base" and "commercial") that
match the version of Python that you are using, and running them. What
docs are you reading?

Cheers,
John
Phill Atwood
2006-03-14 00:19:21 UTC
Permalink
Post by John Machin
Sorry, you've lost me ... I thought "this" (installing mxODBC) was as
simple as downloading the two installers ("base" and "commercial")
that match the version of Python that you are using, and running them.
What docs are you reading?
http://phplens.com/lens/adodb/adodb-py-docs.htm#databases

I guess this is more PHP oriented...

I'm not sure what you are referring to when you say 'two installers
"base" and "commercial"'. I downloaded the adodb zip file and followed
the instructions to
python setup.py install

it. Which is what I did. According to someone else this is all I need do...

Thanks for your input.
Phill
John Machin
2006-03-14 00:54:38 UTC
Permalink
Post by Phill Atwood
Post by John Machin
Sorry, you've lost me ... I thought "this" (installing mxODBC) was as
simple as downloading the two installers ("base" and "commercial")
that match the version of Python that you are using, and running them.
What docs are you reading?
http://phplens.com/lens/adodb/adodb-py-docs.htm#databases
I guess this is more PHP oriented...
I'm not sure what you are referring to when you say 'two installers
"base" and "commercial"'.
You wrote "But I guess I
need to install mxODBC as well. But I'm not quite understanding the
docs I'm reading on how to do this."

I.e. IMHO the only reasonable construction was that you were asking how
to install mxODBC. That's what I replied to. You need to install mx base
to get the date/time stuff, and mx commercial to get mxODBC.
Tim Roberts
2006-03-13 23:20:11 UTC
Permalink
Post by Phill Atwood
Thanks. I've downloaded and installed adodb for Python. But I guess I
need to install mxODBC as well. But I'm not quite understanding the
docs I'm reading on how to do this. It seems very complex....
No, you don't need mxODBC, although I'm curious to know what led you to
believe that. Go do a google search for "adodb connection strings", and
you find several samples. Here's a simple sample that I used to access
an Access database. Note that I have connection strings for either the
Jet OLEDB driver, or the Access ODBC driver. This opens a "table"
recordset (the rs.Open function).

import os
import win32com.client

conn = win32com.client.Dispatch("ADODB.Connection")

# Either way works: one is the Jet OLEDB driver, the other is the
# Access ODBC driver. OLEDB is probably better.

db = r"c:\dev\54nsdc\Volunteer.mdb"
DSN="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + db
#DSN="Driver={Microsoft Access Driver (*.mdb)};DBQ=" + db
conn.Open(DSN)

rs = win32com.client.Dispatch("ADODB.Recordset")
rs.Open( "[Committees]", conn, 1, 3 )

print rs.Fields.Count, " fields found:"
for x in range(rs.Fields.Count):
print rs.Fields.Item(x).Name,
print rs.Fields.Item(x).Type,
print rs.Fields.Item(x).DefinedSize,
print rs.Fields.Item(x).Value



To execute a generic SQL statement, you create an ADODB.Command object
and connect it to the Connection:

cmd = win32com.client.Dispatch("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = "SELECT COUNT(*) FROM committees;"
rs = cmd.Execute[0]


Now rs is a recordset.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Phill Atwood
2006-03-13 23:54:07 UTC
Permalink
Post by Tim Roberts
No, you don't need mxODBC, although I'm curious to know what led you
to believe that.
http://phplens.com/lens/adodb/adodb-py-docs.htm#databases

I guess maybe this link was referring to PHP ...

Well, painfully I'm making some progress. And I appreciate your help in
this. I think my previous problem was because I was doing

rs.Open(sqlstmt)

instead of:

rs.Open(sqlstmt, conn)
Post by Tim Roberts
To execute a generic SQL statement, you create an ADODB.Command object
cmd = win32com.client.Dispatch("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = "SELECT COUNT(*) FROM committees;"
rs = cmd.Execute[0]
Now rs is a recordset.
Now, I've got your above example working. Almost. For me

rs = cmd.Execute[0]

failed with

TypeError: unsubscriptable object

However, trying this variation:

(rs, result) = cmd.Execute()

did work. Problem is when I then try

data = rs.GetRows()

I get

Traceback (most recent call last):
File "<pyshell#59>", line 1, in -toplevel-
data = rs.GetRows()
File
"C:\Python24\lib\site-packages\win32com\gen_py\2A75196C-D9EB-4129-B803-931327F72D5Cx0x2x8.py",
line 2333, in GetRows
, Start, Fields)
File "C:\Python24\Lib\site-packages\win32com\client\__init__.py", line
446, in _ApplyTypes_
return self._get_good_object_(
com_error: (-2147352567, 'Exception occurred.', (0, 'ADODB.Recordset',
'Operation is not allowed when the object is closed.',
'C:\\WINDOWS\\HELP\\ADO270.CHM', 1240653, -2146824584), None)



I feel so tantillizingly close...

Phill
Tim Roberts
2006-03-14 00:26:56 UTC
Permalink
Post by Phill Atwood
Well, painfully I'm making some progress. And I appreciate your help
in this. I think my previous problem was because I was doing
rs.Open(sqlstmt)
rs.Open(sqlstmt, conn)
Post by Tim Roberts
To execute a generic SQL statement, you create an ADODB.Command
cmd = win32com.client.Dispatch("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = "SELECT COUNT(*) FROM committees;"
rs = cmd.Execute[0]
Now rs is a recordset.
Now, I've got your above example working. Almost. For me
rs = cmd.Execute[0]
failed with
TypeError: unsubscriptable object
Duh, yes, I typed this from my code instead of cutting and pasting.
rs = cmd.Execute()[0]
is what I meant, which does the same as what you have.
Post by Phill Atwood
Problem is when I then try
data = rs.GetRows()
I get
File "<pyshell#59>", line 1, in -toplevel-
data = rs.GetRows()
File
"C:\Python24\lib\site-packages\win32com\gen_py\2A75196C-D9EB-4129-B803-931327F72D5Cx0x2x8.py",
line 2333, in GetRows
, Start, Fields)
File "C:\Python24\Lib\site-packages\win32com\client\__init__.py",
line 446, in _ApplyTypes_
return self._get_good_object_(
com_error: (-2147352567, 'Exception occurred.', (0, 'ADODB.Recordset',
'Operation is not allowed when the object is closed.',
'C:\\WINDOWS\\HELP\\ADO270.CHM', 1240653, -2146824584), None)
I'm not sure why you'd use GetRows.

while not rs.EOF:
print rs.Fields("FieldName1")
rs.MoveNext()

I often write a small wrapper around the recordset which turns it into a
generator, because inevitably, on the first round, I forget the MoveNext
and get an infinite loop:

def Recordset(cmd, sql):
cmd.CommandText = sql
rs = cmd.Execute()[0]
while not rs.EOF:
yield rs
rs.MoveNext()

for row in Recordset( cmd, "SELECT FieldName1 FROM MyTable" ):
print rs.Fields("FieldName1")
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Phill Atwood
2006-03-14 01:13:04 UTC
Permalink
Post by Tim Roberts
print rs.Fields("FieldName1")
rs.MoveNext()
This isn't working for me either. Same error about

error: (-2147352567, 'Exception occurred.', (0, 'ADODB.Recordset',
'Operation is not allowed when the object is closed.',
'C:\\WINDOWS\\HELP\\ADO270.CHM', 1240653, -2146824584), None)
the object being closed. I'm not sure why it thinks that. If I print
rs it describes an instance etc. and my result value from (rs, result)
is 35 which is exactly the number of records updated by the query.
Could it have something to do with the fact that the query updates
another table? Do I need to look at that table? Is that why rs is
empty or closed. Bizarre.

Phill
Tim Roberts
2006-03-14 01:26:01 UTC
Permalink
Post by Phill Atwood
Post by Tim Roberts
print rs.Fields("FieldName1")
rs.MoveNext()
This isn't working for me either. Same error about
error: (-2147352567, 'Exception occurred.', (0, 'ADODB.Recordset',
'Operation is not allowed when the object is closed.',
'C:\\WINDOWS\\HELP\\ADO270.CHM', 1240653, -2146824584), None)
the object being closed. I'm not sure why it thinks that. If I print
rs it describes an instance etc. and my result value from (rs,
result) is 35 which is exactly the number of records updated by the
query. Could it have something to do with the fact that the query
updates another table? Do I need to look at that table? Is that why
rs is empty or closed. Bizarre.
Well, yes, of course. Action queries (UPDATE, DELETE, INSERT) don't
return a recordset. Only SELECT queries do. If all you're trying to do
is an UPDATE, then just do the Execute() and you're done.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Phill Atwood
2006-03-18 23:33:34 UTC
Permalink
I am using os.system(cmd) to call a DOS program on WinXP that I need to
call. It works fine but it flashes a Command Prompt Window while it
does it. Is there any way to suppress this?
Shane Holloway (IEEE)
2006-03-19 00:46:00 UTC
Permalink
I'd suggest looking at the subprocess module if you are using python
2.4, or one of the many os.popen variants for prior versions.
Post by Phill Atwood
I am using os.system(cmd) to call a DOS program on WinXP that I need to
call. It works fine but it flashes a Command Prompt Window while it
does it. Is there any way to suppress this?
_______________________________________________
Python-win32 mailing list
http://mail.python.org/mailman/listinfo/python-win32
Phill Atwood
2006-03-19 01:38:22 UTC
Permalink
Thanks, using subprocess.call(cmd, shell=True) worked for me without
flashing anything.

Phill
Post by Shane Holloway (IEEE)
I'd suggest looking at the subprocess module if you are using python
2.4, or one of the many os.popen variants for prior versions.
Post by Phill Atwood
I am using os.system(cmd) to call a DOS program on WinXP that I need to
call. It works fine but it flashes a Command Prompt Window while it
does it. Is there any way to suppress this?
_______________________________________________
Python-win32 mailing list
http://mail.python.org/mailman/listinfo/python-win32
Loading...