473,499 Members | 1,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

conecting with a MsAcces DB by dao

Hi
I'm using activestate python 2.4 on win xp 2 ed. and Ms Access 2002
(reading first http://starship.python.net/crew/bwilk/access.html)
I have writed the following code

def append_from_Access(self):
try:
import ...
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=C:/Afile.mdb;"
conn.Open(DSN)
except Exception, inst:
...
try:
sql_statement='SELECT * FROM Mytable'
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(sql_statement, conn, 1, 3)
while not rs.EOF:
id=rs.Fields(colName.Value) #colName, valid column name
...
rs.MoveNext()
rs.Close()
conn.Close()

except Exception, inst:
...

I'm using it for reading tables or queries in a mdb file.
With some mdb it works fine and return a no empty recordset, but with
others mdb files, the recordsets are void (opening the tables or
recorsets with Ms Access are not void).
Some help is welcome,
Thanks in advance
Luis

Jun 30 '06 #1
8 3271

luis wrote:
Hi
I'm using activestate python 2.4 on win xp 2 ed. and Ms Access 2002
(reading first http://starship.python.net/crew/bwilk/access.html)
I have writed the following code

def append_from_Access(self):
try:
import ...
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=C:/Afile.mdb;"
conn.Open(DSN)
except Exception, inst:
...
try:
sql_statement='SELECT * FROM Mytable'
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(sql_statement, conn, 1, 3)
while not rs.EOF:
id=rs.Fields(colName.Value) #colName, valid column name
...
rs.MoveNext()
rs.Close()
conn.Close()

except Exception, inst:
...

I'm using it for reading tables or queries in a mdb file.
With some mdb it works fine and return a no empty recordset, but with
others mdb files, the recordsets are void (opening the tables or
recorsets with Ms Access are not void).
Some help is welcome,
Thanks in advance
Luis


I don't know if it's the problem your asking about, but your
rs.MoveNext() should be inside the while loop, no?

Iain

Jun 30 '06 #2

Iain King ha escrito:
luis wrote:
Hi
I'm using activestate python 2.4 on win xp 2 ed. and Ms Access 2002
(reading first http://starship.python.net/crew/bwilk/access.html)
I have writed the following code

def append_from_Access(self):
try:
import ...
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=C:/Afile.mdb;"
conn.Open(DSN)
except Exception, inst:
...
try:
sql_statement='SELECT * FROM Mytable'
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(sql_statement, conn, 1, 3)
while not rs.EOF:
id=rs.Fields(colName.Value) #colName, valid column name
...
rs.MoveNext()
rs.Close()
conn.Close()

except Exception, inst:
...

I'm using it for reading tables or queries in a mdb file.
With some mdb it works fine and return a no empty recordset, but with
others mdb files, the recordsets are void (opening the tables or
recorsets with Ms Access are not void).
Some help is welcome,
Thanks in advance
Luis
I don't know if it's the problem your asking about, but your
rs.MoveNext() should be inside the while loop, no?

Yes, is inside
Iain


Jun 30 '06 #3

luis wrote:
Iain King ha escrito:
luis wrote:
while not rs.EOF:
id=rs.Fields(colName.Value) #colName, valid column name
...
rs.MoveNext()
rs.Close()
conn.Close()


I don't know if it's the problem your asking about, but your
rs.MoveNext() should be inside the while loop, no?

Yes, is inside


You mean, it is inside the while loop in your code, but you made a
mistake copying it into your post? In the code you posted it is not
inside the while loop - it would have to be indented one more level for
that.

Iain

Jun 30 '06 #4

Iain King ha escrito:
luis wrote:
Iain King ha escrito:
luis wrote:
> while not rs.EOF:
> id=rs.Fields(colName.Value) #colName, valid column name
> ...
> rs.MoveNext()
> rs.Close()
> conn.Close()

I don't know if it's the problem your asking about, but your
rs.MoveNext() should be inside the while loop, no?

Yes, is inside


You mean, it is inside the while loop in your code, but you made a
mistake copying it into your post? In the code you posted it is not
inside the while loop - it would have to be indented one more level for
that.

Iain


this is te correct identation

def append_from_Access(self):
try:
import ...
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=C:/Afile.mdb;"
conn.Open(DSN)
except Exception, inst:
...
try:
sql_statement='SELECT * FROM Mytable'
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(sql_statement, conn, 1, 3)
while not rs.EOF:
id=rs.Fields(colName.Value) #colName, valid column name
...
rs.MoveNext()
rs.Close()
conn.Close()

except Exception, inst:
...

I think my problem must be with ado and dao.
Now I have run makepy utility and select Microsoft ActiveX Data Objects
2.5 Library, perhaps I must also select Microsoft DAO3.5 Object Library
and write
win32com.client.Dispatch("DAO.DBEngine.35") for Access 97 or
win32com.client.Dispatch(r'ADODB.Connection') for Acess 2000
Do you know is it possible ?
luis

Jun 30 '06 #5

luis wrote:
Iain King ha escrito:
luis wrote:
Iain King ha escrito:

> luis wrote:
> > while not rs.EOF:
> > id=rs.Fields(colName.Value) #colName, valid column name
> > ...
> > rs.MoveNext()
> > rs.Close()
> > conn.Close()
>
> I don't know if it's the problem your asking about, but your
> rs.MoveNext() should be inside the while loop, no?
Yes, is inside
>


You mean, it is inside the while loop in your code, but you made a
mistake copying it into your post? In the code you posted it is not
inside the while loop - it would have to be indented one more level for
that.

Iain


this is te correct identation

def append_from_Access(self):
try:
import ...
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=C:/Afile.mdb;"
conn.Open(DSN)
except Exception, inst:
...
try:
sql_statement='SELECT * FROM Mytable'
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(sql_statement, conn, 1, 3)
while not rs.EOF:
id=rs.Fields(colName.Value) #colName, valid column name
...
rs.MoveNext()
rs.Close()
conn.Close()

except Exception, inst:
...

I think my problem must be with ado and dao.
Now I have run makepy utility and select Microsoft ActiveX Data Objects
2.5 Library, perhaps I must also select Microsoft DAO3.5 Object Library
and write
win32com.client.Dispatch("DAO.DBEngine.35") for Access 97 or
win32com.client.Dispatch(r'ADODB.Connection') for Acess 2000
Do you know is it possible ?
luis


Well, without being able to test on your system I don't think I can
give you any real advice. This is the module I use to interface with
Access:

Access.py
---------------
import win32com.client
from win32com.client import constants

def isWriteable(field):
"""Is given Field writeable?"""
return field.Attributes & 4
class Access(object):
def __init__(self, filename, password=""):
self._filename = filename
self._connection = win32com.client.Dispatch(r'ADODB.Connection')
if password:
self._DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=%s;Jet
OLEDB:Database Password=%s;' % (filename, password)
else:
self._DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=%s;' %
(filename)

def Query(self, query):
self._connection.Open(self._DSN)
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(query, self._connection, 1, 3)
fields = []
for x in xrange(rs.Fields.Count):
fields.append(rs.Fields(x).Name)
if rs.EOF:
data = []
else:
data = rs.GetRows()
rs.Close()
self._connection.Close()
return fields, data
def Add(self, table, records):
"""Adds records to table."""
self._connection.Open(self._DSN)
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(table, self._connection, 1, 3)
unwriteables = []
for record in records:
rs.AddNew()
unwriteable = []
for i in xrange(len(record)):
if isWriteable(rs.Fields(i)):
rs.Fields(i).Value = record[i]
else:
unwriteable.append(rs.Fields(i).Value)
unwriteables.append(unwriteable)
rs.Update()
rs.Close()
self._connection.Close()
return unwriteables
def Update(self, query, function):
"""Updates all records found in query with function(record)"""
self._connection.Open(self._DSN)
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(query, self._connection, 1, 3)
columns = rs.Fields.Count
while not rs.EOF:
record = []
for i in xrange(columns):
record.append(rs.Fields(i).Value)
newRecord = function(record[:])
for i in xrange(columns):
if isWriteable(rs.Fields(i)):
rs.Fields(i).Value = newRecord[i]
rs.MoveNext()
rs.Close()
self._connection.Close()
def Delete(self, query):
"""Deletes all records found in query"""
self._connection.Open(self._DSN)
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(query, self._connection, 1, 3)
while not rs.EOF:
rs.Delete()
rs.MoveNext()
rs.Close()
self._connection.Close()
We only have Access 2000+, so I don't know if it'll work with prior
versions.

Iain

Jun 30 '06 #6
Iain King ha escrito:
luis wrote:
Iain King ha escrito:
luis wrote:
Iain King ha escrito:

luis wrote:
while not rs.EOF:
id=rs.Fields(colName.Value) #colName, valid column name
...
rs.MoveNext()
rs.Close()
conn.Close()
>
I don't know if it's the problem your asking about, but your
rs.MoveNext() should be inside the while loop, no?
Yes, is inside
>
>
You mean, it is inside the while loop in your code, but you made a
mistake copying it into your post? In the code you posted it is not
inside the while loop - it would have to be indented one more level for
that.
>
Iain
this is te correct identation

def append_from_Access(self):
try:
import ...
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=C:/Afile.mdb;"
conn.Open(DSN)
except Exception, inst:
...
try:
sql_statement='SELECT * FROM Mytable'
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(sql_statement, conn, 1, 3)
while not rs.EOF:
id=rs.Fields(colName.Value) #colName, valid column name
...
rs.MoveNext()
rs.Close()
conn.Close()

except Exception, inst:
...

I think my problem must be with ado and dao.
Now I have run makepy utility and select Microsoft ActiveX Data Objects
2.5 Library, perhaps I must also select Microsoft DAO3.5 Object Library
and write
win32com.client.Dispatch("DAO.DBEngine.35") for Access 97 or
win32com.client.Dispatch(r'ADODB.Connection') for Acess 2000
Do you know is it possible ?
luis

Well, without being able to test on your system I don't think I can
give you any real advice. This is the module I use to interface with
Access:

Access.py
---------------
import win32com.client
from win32com.client import constants

def isWriteable(field):
"""Is given Field writeable?"""
return field.Attributes & 4
class Access(object):
def __init__(self, filename, password=""):
self._filename = filename
self._connection = win32com.client.Dispatch(r'ADODB.Connection')
if password:
self._DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=%s;Jet
OLEDB:Database Password=%s;' % (filename, password)
else:
self._DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=%s;' %
(filename)

def Query(self, query):
self._connection.Open(self._DSN)
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(query, self._connection, 1, 3)
fields = []
for x in xrange(rs.Fields.Count):
fields.append(rs.Fields(x).Name)
if rs.EOF:
data = []
else:
data = rs.GetRows()
rs.Close()
self._connection.Close()
return fields, data
def Add(self, table, records):
"""Adds records to table."""
self._connection.Open(self._DSN)
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(table, self._connection, 1, 3)
unwriteables = []
for record in records:
rs.AddNew()
unwriteable = []
for i in xrange(len(record)):
if isWriteable(rs.Fields(i)):
rs.Fields(i).Value = record[i]
else:
unwriteable.append(rs.Fields(i).Value)
unwriteables.append(unwriteable)
rs.Update()
rs.Close()
self._connection.Close()
return unwriteables
def Update(self, query, function):
"""Updates all records found in query with function(record)"""
self._connection.Open(self._DSN)
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(query, self._connection, 1, 3)
columns = rs.Fields.Count
while not rs.EOF:
record = []
for i in xrange(columns):
record.append(rs.Fields(i).Value)
newRecord = function(record[:])
for i in xrange(columns):
if isWriteable(rs.Fields(i)):
rs.Fields(i).Value = newRecord[i]
rs.MoveNext()
rs.Close()
self._connection.Close()
def Delete(self, query):
"""Deletes all records found in query"""
self._connection.Open(self._DSN)
rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.Open(query, self._connection, 1, 3)
while not rs.EOF:
rs.Delete()
rs.MoveNext()
rs.Close()
self._connection.Close()
We only have Access 2000+, so I don't know if it'll work with prior
versions.

Iain
Thanks, for your code

My problem was opening a query (not a table) on mdb file

1) If the query includes a where clause type
field1="edf" and field2=3
, for example, no problem, Access can retrieve a not empty recordset
and my python code too.

2) But if the Access's query includes a LIKE clause, for example
field1="e*"
, Access can retrieves a not empty recordset but my python code
retrieves a empty recordset.

If I write the WHERE clause inside my python code, the recorset returns
an non empty recordset. Perhaps the problem was the different use of *
and % in Access and SQL, I don't know.

Luis

Jul 3 '06 #7
luis wrote:
My problem was opening a query (not a table) on mdb file

1) If the query includes a where clause type
field1="edf" and field2=3
, for example, no problem, Access can retrieve a not empty recordset
and my python code too.

2) But if the Access's query includes a LIKE clause, for example
field1="e*"
, Access can retrieves a not empty recordset but my python code
retrieves a empty recordset.
Which is exactly what it _should_ return on that query, unless you have
records with "e*" in column field1.

The proper SQL clause is:

... WHERE field1 LIKE 'e*' ...

But, you did not include that part of the program, so you wound up
crippling those who were willing to try to help you, because you
thought you kinda-sorta knew what was going on (but not enough to
fix it). Find "smart questions" and read it a couple of times.
--
--Scott David Daniels
sc***********@acm.org
Jul 3 '06 #8
>, Access can retrieves a not empty recordset but my python code
>retrieves a empty recordset.

Which is exactly what it _should_ return on that query, unless you have
records with "e*" in column field1.

The proper SQL clause is:

... WHERE field1 LIKE 'e*' ...

which would also return an empty recordset unless the OP has
records with "e*" in column field1...

the proper SQL clause would be

... WHERE field1 LIKE 'e%' ...

(in SQL, the "%" and "_" are used as wildcards, not asterisk and
question-mark respectively as they are in most shell expansions)

:)

-tkc

Jul 3 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1147
by: AG | last post by:
I want to connect over the internet to get data from clients.What would be the easiest method and the most stable methos to do it 1.Use a Point to Point prtotcol from the client pc and then...
1
1290
by: zumbar | last post by:
hi, I am writing a java application that connects to a MS Access databse with two fields , Date and amount. I can insert into this datbase using the application. Now , I need to retrieve weekly...
2
4408
by: amjad | last post by:
Hi when i try below code public DataSet GetData(string ConString) { OracleDataAdapter da; DataSet ds = new DataSet(); // Setup connection string to access Oracle 9i
4
1835
by: PraveenKadkol | last post by:
Hi, My Development is in almost final stages, now i am facing problem with the reports. I am generating MsAccess Reports based on the selected Parameters, while generating Report I am opening ...
2
1286
by: =?Utf-8?B?cGVsZWdrMQ==?= | last post by:
i am trying to conenct oto sql server 2005 when i do it i the VS2005 every is ok but when i run from the browser i get : The login failed. Login failed for user 'JUSTATHOME\ASPNET'. i read this...
0
908
by: pradeep84 | last post by:
hi.. to all.. i hav created the applet program codes inorder to save the data in the database.... now i hav to view the data using applets.. any specific method is there.... to do that.. ...
2
1434
daffurankan
by: daffurankan | last post by:
HAi , this ankan wanted to know the code how can i change access database password through vb 6.0 code.
0
1064
by: laurence | last post by:
hi, i am new in vb. i am not finding the way to connect ms access database to visual basic. what i need is i need to take data from msaccess and calculation will be done in vb and the result page...
1
1162
by: ZyON | last post by:
I wrote this code in asp.net(vb): dim dbconn as New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("../kri1.mdb")) Dim DBCmd As New OleDbCommand Dim DBAdap As...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.