[Zope-dev] Programmatically accessing Z SQL Methods

Paulo Eduardo Neves neves@inf.puc-rio.br
Thu, 04 Mar 1999 18:42:19 -0300


Mike Pelletier wrote:
> 
>     I would like the user to select the Database Connection when my Product is
> being added. 

Take a look at the function SQLConnectionIDs(self) in
lib/python//Products/ZSQLMethods/SQL.py

It selects all avaiable Database conections ids.

> When the Product installs itself, I want it to check the
> database it's been passed for the existance of a particular table.  If it does
> not exist, I want to create said table and do some initial populating.

Some databases connectors have a standard api for querying tables and
columns. The tab browse calls it. The problem is that not all DB
connectors completely implements the api. The MySQL one, for example,
doesn't implements it. With it you have to directly query the DB. 

If you have the connection id, you get the connection with:
connection = getattr(self, self.connection_id)

self is your product.

Connection is an object that implements a __call__ method. You should
make your query:
dummy, result = self.connection().query(command)

Oops! The query method returns a tuple, I don't remember what is the
first returned value. I didn't used it. See the source.

>     So far my scheme for doing this involves adding a Z SQL Method to the
> current folder, putting some SQL in it, running it, putting some SQL in it,
> running it, etc, etc, and then removing it when I'm done.  This seems like a
> stupid way to go about things.  I'd much rather just be able to take my
> connection and submit some SQL programmatically.  So, I guess question is
> simply, "How do I submit an SQL query, starting from a connection_id?"

I've did somenthing like it in the PMySQLInput wizard, see it in the
contrib area. 

hope this helps,
-- 
Paulo Eduardo Neves
PUC-Rio de Janeiro 
Pager: Central: 292-4499 cod. 213 99 64
ou use a URL:  http://www.learn.fplf.org.br/neves/mensagempager.html