[Webware-discuss] ADO in WebWare

Jeff Johnson wrote:
> I would love that! I tried to get ADO to work a few weeks ago but other
> projects took priority. Any help about using ADO from Python would be
> greatly appreciated.
First of all, I recommend getting the "Python Programming on Win32" book by Mark
Hammond and Andy Robinson. I've found it to be immensely useful.
In order to use any COM objects from a multithreaded application such as WebKit,
you have to do some COM initialization magic. The easiest way to do this is to
create a subdirectory underneath your WebWare directory called Win32Kit. Place
the attached file __init__.py into that directory.
You can now magically use COM objects from your WebKit servlets.
You'll want to install MDAC 2.5 which is downloadable from Microsoft -- this
gives you the latest version of ADO with drivers for SQL Server and other
databases.
Run the COM Makepy utility inside of PythonWin on "Microsoft ActiveX Data
Objects 2.5 Library."
Create an ODBC System Data Source for your SQL Server. This can be done in the
Windows Control Panel. The example code below assumes you called it MyDatabase.
Now you're all set -- you can use ADO from your servlets. It's not hard to just
use the ADO objects by themselves, but I've written some helper classes that
make it easier. It's called DatabaseMixin.py -- you can just put it in the same
directory as your servlets if you want.
I recommend installing the mxDateTime package -- it makes time manipulation much
easier. DatabaseMixin.py assumes you have it installed.
Now, you can write servlets like the following:
from Page import Page
from DatabaseMixin import DatabaseMixin
from WebUtils.WebFuncs import HTMLEncode
class ChooseCustomer(Page, DatabaseMixin):
def writeBody(self):
self.writeln('<H4>Choose a Customer to work with:</H4>')
rs = self.recordset('SELECT CustomerID, CustomerName FROM '
'Customers ORDER BY CustomerName')
for record in rs:
self.writeln('<br><a href="ChooseReport?CustomerID=%d">%s</a>' %
(record.CustomerID, HTMLEncode(record.CustomerName)))
That's about it. Give it a try, and let me know if you have problems,
questions, comments, suggestions, improvements, etc. I've glossed over some
details here in the interest of brevity, but I'd be happy to answer any
questions.
--
- Geoff Talvola
Parlance Corporation
gtalvola@...