472,351 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,351 software developers and data experts.

Access sql with python, please. I'm desperate

DD
Hello,

Could anyone please help me??
Is there somebody who could explain me how to make a connection to a access
database with a python cgi script.
I would like to use common sql commands in my python scripts as I can with
MySQLdb.
But I cannot even connect to the access database (see below).
Could anyone explain it to me as simple as possible please. I'm using
Windows XP, ActivePython 2.3.2 build 230 and Microsoft access(XP?)
I normally use Linux, but this has to be in MS office
import win32api
import win32com.client
engine = win32com.client.Dispatch("DAO.DBEngine.35")

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python23\Lib\site-packages\win32com\client\__init__.py", line 95,
in Dispatch
dispatch, userName =
dynamic._GetGoodDispatchAndUserName(dispatch,userN ame,clsctx)
File "C:\Python23\Lib\site-packages\win32com\client\dynamic.py", line 84,
in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python23\Lib\site-packages\win32com\client\dynamic.py", line 72,
in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx,
pythoncom.IID_IDispatch)
com_error: (-2147221230, 'Klasse is niet gelicenseerd voor gebruik', None,
None)

thank in advance,

Arjen
Jul 18 '05 #1
9 3429
Have you tried using the adodbapi module?

It wraps ADO on win32 platforms, and exposes the DBAPI 2.0 spec,
thus you can use the same sort of calls as you did using MySQLdb.

See:
http://adodbapi.sourceforge.net/

If you still want to go the COM route, I'd suggest at least using
ADO and not DAO.

~Jon Franz
NeuroKode Labs, LLC

----- Original Message -----
From: "DD" <ad@ad.nl>
To: <py*********@python.org>
Sent: Wednesday, November 12, 2003 6:07 PM
Subject: Access sql with python, please. I'm desperate

Hello,

Could anyone please help me??
Is there somebody who could explain me how to make a connection to a access database with a python cgi script.
I would like to use common sql commands in my python scripts as I can with
MySQLdb.
But I cannot even connect to the access database (see below).
Could anyone explain it to me as simple as possible please. I'm using
Windows XP, ActivePython 2.3.2 build 230 and Microsoft access(XP?)
I normally use Linux, but this has to be in MS office
import win32api
import win32com.client
engine = win32com.client.Dispatch("DAO.DBEngine.35")
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python23\Lib\site-packages\win32com\client\__init__.py", line

95, in Dispatch
dispatch, userName =
dynamic._GetGoodDispatchAndUserName(dispatch,userN ame,clsctx)
File "C:\Python23\Lib\site-packages\win32com\client\dynamic.py", line 84, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python23\Lib\site-packages\win32com\client\dynamic.py", line 72, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx,
pythoncom.IID_IDispatch)
com_error: (-2147221230, 'Klasse is niet gelicenseerd voor gebruik', None,
None)

thank in advance,

Arjen

Jul 18 '05 #2
"DD" <ad@ad.nl> wrote:

Could anyone please help me??
Is there somebody who could explain me how to make a connection to a access
database with a python cgi script.
...
Could anyone explain it to me as simple as possible please. I'm using
Windows XP, ActivePython 2.3.2 build 230 and Microsoft access(XP?)
I normally use Linux, but this has to be in MS office
engine = win32com.client.Dispatch("DAO.DBEngine.35")

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
...
File "C:\Python23\Lib\site-packages\win32com\client\dynamic.py", line 72,
in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx,
pythoncom.IID_IDispatch)
com_error: (-2147221230, 'Klasse is niet gelicenseerd voor gebruik', None,
None)


The error code (-2147221230 is 80040112) and the word "gelicenseerd"
indicates that you don't have Microsoft Access or Microsoft Visual Basic
installed on your system. If you DO have one of them installed, there are
instructions in the Microsoft knowledge base in article number 240377 on
how to fix up the registry.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #3
sdd
DD wrote:
Hello,
But I cannot even connect to the access database (see below).
Could anyone explain it to me as simple as possible please. I'm using
Windows XP, ActivePython 2.3.2 build 230 and Microsoft access(XP?)

OK, as several others have said, one solution (and the one I know)
is via ODBC. I use Win2K, not XP; there will be slight differences.

First, we create and ODBC (Open Data Base Connection) DSN (Data Source Name)
I use (forgive the notation; I don't know a generally accepted one):
<Start>
<Settings>
<Control Panel>
<Administrative Tools>
<Data Sources (ODBC)>
Gives a tabbed window labeled "ODBC Data Source Administrator".
Choose tab "User DSN" (or "System DSN") and press the "Add..." button.
It asks you to:
"Select a driver for which you want to set up a data source."

If you cannot find the menus I discuss above, explore using:
<Start>
<Help>
Got to tab "Search" and type "ODBC" without the quotes. Check out
the various subjects that follow.
Choose some version of "Microsoft Access Driver (*.mdb)" -- my
machine offers the choice in a couple of human languages; I
inevitably go for English (my native language), but your
performance my vary.
Having chose the correct driver, I click "Finish"
This takes me to a dialog "ODBC Microsoft Access Setup"
That wants a Data Source Name (name by which I will later refer
to the DB) and a Description. I always choose a simple DSN
such as "Exploratory" and skip the Description, but I am on a home
machine. Remember the DSN namespace is "flat" -- no directories;
all the names you use must be distinct. So for my example we use
"Exploratory" for the DSN and "None of your business" for the
description. In the "Database" box, I click "Select...", which
will make a file chooser appear. I then navigate to my access
database's file (c:\imports\dbxx.mdb in this case) and click OK.
Under the options section, I'm clicking "Read Only", but I'm paranoid.
I now click OK here and again at the "ODBC Data Source Administrator"
window, and this dialog session is at an end.

From your attempts, I see you have loaded the win32 extensions, so
the following should work work for you as well (changing names as
appropriate -- I have a table named category):

import odbc
conn = odbc.odbc("Exploratory")
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM category")
print "lines in category = %s" % cursor.fetchone()
cursor.close()
conn.close()

If you get this far you are in business. You can use idle (or your
other favorite python interactive environment) to explore the results
you get. Know the difference between fetchone, fetchall and fetchmany,
understand setautocommit, and have fun.

I personally used mxODBC
http://www.lemburg.com/files/python/mxODBC.html

to do a lot of Database work at one job; Lemburg dots 'i's and
crosses 't's to give you a great exploratory database environment.
You get column names for all row returned and so on. The odbc
package that came with win32 does the bare bones. The difference
is well worth the price in time you save, in deployed systems
(especially those given away) you may have to give up the features
Marc-Andre provides.

-Scott David Daniels
Sc***********@Acm.Org

Jul 18 '05 #4
dd
Thank you very much. It really helped. By the way, can one do the
following?

select * from [a-query] where .......

So, select from a query instead of a table...

Thanks anyway you guys, for answering the previous question.
Arjen

DD wrote:
Hello,
But I cannot even connect to the access database (see below).
Could anyone explain it to me as simple as possible please. I'm using
Windows XP, ActivePython 2.3.2 build 230 and Microsoft access(XP?)

OK, as several others have said, one solution (and the one I know)
is via ODBC. I use Win2K, not XP; there will be slight differences.

First, we create and ODBC (Open Data Base Connection) DSN (Data Source Name)
I use (forgive the notation; I don't know a generally accepted one):
<Start>
<Settings>
<Control Panel>
<Administrative Tools>
<Data Sources (ODBC)>
Gives a tabbed window labeled "ODBC Data Source Administrator".
Choose tab "User DSN" (or "System DSN") and press the "Add..." button.
It asks you to:
"Select a driver for which you want to set up a data source."

If you cannot find the menus I discuss above, explore using:
<Start>
<Help>
Got to tab "Search" and type "ODBC" without the quotes. Check out
the various subjects that follow.
Choose some version of "Microsoft Access Driver (*.mdb)" -- my
machine offers the choice in a couple of human languages; I
inevitably go for English (my native language), but your
performance my vary.
Having chose the correct driver, I click "Finish"
This takes me to a dialog "ODBC Microsoft Access Setup"
That wants a Data Source Name (name by which I will later refer
to the DB) and a Description. I always choose a simple DSN
such as "Exploratory" and skip the Description, but I am on a home
machine. Remember the DSN namespace is "flat" -- no directories;
all the names you use must be distinct. So for my example we use
"Exploratory" for the DSN and "None of your business" for the
description. In the "Database" box, I click "Select...", which
will make a file chooser appear. I then navigate to my access
database's file (c:\imports\dbxx.mdb in this case) and click OK.
Under the options section, I'm clicking "Read Only", but I'm paranoid.
I now click OK here and again at the "ODBC Data Source Administrator"
window, and this dialog session is at an end.

From your attempts, I see you have loaded the win32 extensions, so
the following should work work for you as well (changing names as
appropriate -- I have a table named category):

import odbc
conn = odbc.odbc("Exploratory")
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM category")
print "lines in category = %s" % cursor.fetchone()
cursor.close()
conn.close()

If you get this far you are in business. You can use idle (or your
other favorite python interactive environment) to explore the results
you get. Know the difference between fetchone, fetchall and fetchmany,
understand setautocommit, and have fun.

I personally used mxODBC
http://www.lemburg.com/files/python/mxODBC.html

to do a lot of Database work at one job; Lemburg dots 'i's and
crosses 't's to give you a great exploratory database environment.
You get column names for all row returned and so on. The odbc
package that came with win32 does the bare bones. The difference
is well worth the price in time you save, in deployed systems
(especially those given away) you may have to give up the features
Marc-Andre provides.

-Scott David Daniels
Sc***********@Acm.Org


Jul 18 '05 #5
sdd
dd wrote:
Thank you very much. It really helped. You are welcome. By the way, we try to avoid "top-posting" here
in the c.l.p newsgroup, since it makes the message hard to read.
These messages get searched pretty intensively by people looking
for answers, and we like to keep them readable.
By the way, can one do the following?
select * from [a-query] where .......
So, select from a query instead of a table...


Well, that depends on your database provider (Access in this case),
rather than ODBC. I have two suggestions:

1) (A Python suggestion) Just try it. Fire up Idle, pythonwin,
the python interpreter, or whatever interactive python
you find the most comfortable and type away.

2) (a database guy suggestion) Avoid it if you can. Your query
is best handled if it is in a nice, flat, and-only query. A
query optimizer "wants" to try a lot of different ways to
evaluate your query, and pick the one with the least estimated
cost based on things like indices, estimated sizes, and actual
sizes. The "nested query" form is not trivially transformed
into a form that allows for many rewrites; several cheaper
optimizers just "punt" on such queries and run the obvious way.
That said, on at least some systems you can say:

SELECT * for (SELECT age, weight FROM personnel)
WHERE age in (SELECT age FROM retirees)

-Scott David Daniels
Sc***********@Acm.Org

Jul 18 '05 #6
sdd
I wrote:
That said, on at least some systems you can say:

SELECT * for (SELECT age, weight FROM personnel)
WHERE age in (SELECT age FROM retirees)

When of course I meant to write:
SELECT * FROM (SELECT age, weight FROM personnel)
WHERE age IN (SELECT age FROM retirees)

-Scott David Daniels
Sc***********@Acm.Org

Jul 18 '05 #7
sdd wrote:
I wrote:
That said, on at least some systems you can say:

SELECT * for (SELECT age, weight FROM personnel)
WHERE age in (SELECT age FROM retirees)

When of course I meant to write:
SELECT * FROM (SELECT age, weight FROM personnel)
WHERE age IN (SELECT age FROM retirees)


Yes, any "true" relational DB supports nested queries
(has mySQL gotten around to that yet...?), but I'm not
sure why you want the FIRST of the two nestings here --
why not just one nesting, as in:

SELECT age, weight FROM personnel
WHERE age in (SELECT age FROM retirees)

?
Alex

Jul 18 '05 #8
Alex Martelli wrote:
[...] Yes, any "true" relational DB supports nested queries
(has mySQL gotten around to that yet...?), [...]


Apparently, only in 4.1, the alpha version
(http://www.mysql.com/doc/en/Roadmap.html).

Looks like I can wait a few years before trying out MySQL again ...

-- Gerhard
Jul 18 '05 #9
sdd
Alex Martelli wrote:
sdd wrote:
SELECT * FROM (SELECT age, weight FROM personnel)
WHERE age IN (SELECT age FROM retirees)


...I'm not sure why you want the FIRST of the two nestings here --
why not just one nesting, as in:
SELECT age, weight FROM personnel
WHERE age in (SELECT age FROM retirees)
?

There is no value to the nesting; I was simply trying to
echo the pattern the original poster's nested query in a
syntactically correct way. The fillip was in adding the
plausibly reasonable second subquery.

-Scott David Daniels
Sc***********@Acm.Org

Jul 18 '05 #10

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

Similar topics

1
by: SCS | last post by:
System: Windows 2003 Server PHP 5 Final IIS 6 Problem: Every time I run a PHP page I get "PHP has encountered an Access Violation at...
57
by: John Howard | last post by:
I've sent several messages over the last year asking about python - Who teaches python? Is python losing steam? etc. I have noticed, eg, the...
6
by: John Baker | last post by:
Hi: As those who have looked at this newsgroup recently will realize, I am a neophyte with Access, although I have experienced with Approach (the...
13
by: BigDaDDY | last post by:
Um yeah....In case you haven't figured it out, Microsoft sucks. I'm going to be kicked back in my chair eating popcorn and watching football 10...
5
by: Bec | last post by:
I'm in desperate need of your help.. I need to build an access database and have NO idea how to do this.. Not even where to start.. It IS...
118
by: 63q2o4i02 | last post by:
Hi, I've been thinking about Python vs. Lisp. I've been learning Python the past few months and like it very much. A few years ago I had an AI...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly...
1
by: Charles Wilson | last post by:
Can someone point me to some good instructions on using the WinInet.dll functions from VBA? I need to interact with a MySQL database via HTTP...
32
by: Licheng Fang | last post by:
Basically, the problem is this: 'do' Python's NFA regexp engine trys only the first option, and happily rests on that. There's another...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.