[Zope-DB] Hello!

Jim Penny jpenny@universal-fasteners.com
Fri, 18 Jul 2003 16:26:06 -0400


On Fri, 18 Jul 2003 18:09:47 +0100
Philip Kilner <phil@xfr.co.uk> wrote:

> Hi List,
> 
> Philip Kilner wrote:
> 
> > - How do I call the "insertBook" ZSQL method from within a ZPT?
> > 
> > - Do I have to do anything special to map my field names to the 
> > parameters that the ZSQl expects?
> > 
> > (I've written this partly as a test of whether I understand it
> > myself - I'll fire the email off and go RTFM now!)
> > 
> 
> OK - have now read up on Form Processing in AvZPT in the Zope book.
> 
> Looks like the answer to my first question is "Write a Pythion script
> to call the ZSQL, and call this script from the ZPT". I can suss the
> ZPT syntax from the Zope Book- can anyone point me to a good example
> of Python calling ZSQL?
> 

Hmm.

OK, suppose my_sql_method is a query having parameters foo, and bar and
it lies in the current container.

Foo and bar can be in the request, or they can be passed as named
parameters.  In the first case, the call would simply look like:

res=container.my_sql_method(request)

In the second, it would look like

res=container.my_sql_method(foo=foo_value, bar=bar_value)

where foo_value and bar_value are the values of the parameters that you
want my_sql_method to use.

Now, what is res?

Spinwing has a howto on this, (I have another) but short version is:
res is an object that looks like an array returned rows.

res[0][0] is first row, first column; res[0][1] is first row, 2nd
column; res[1][0] is second row, first column, etc.

len(res) is number of rows returned.

res.names() is the names of the columns.

I like to work with column names, rather than column numbers.  I do
this:

nms=res.names
nm2i={}
for i in range(len(nms)):
  nm2i[nms[i]]=i

Now, if 'baz' is the name of a column, I can index into a row like:

res[0][nm2i['baz']]

(that is, the index of the column named 'baz' in the first row returned.

Thats all there is to it, really.

Jim Penny

> Thanks!
> 
> -- 
> 
> Regards,
> 
> PhilK
> 
> Email: phil@xfr.co.uk / Voicemail & Facsimile: 07092 070518
> 
> "As we enjoy great advantages from inventions of others,
> we should be glad of an opportunity to serve others by
> any invention of ours; and this we should do freely and
> generously." - Benjamin Franklin
> 
>