[Zope-DB] How to import SQL in python script?

eijgnit@netscape.net eijgnit@netscape.net
Mon, 10 Feb 2003 04:51:07 -0500

Charlie Clark <charlie@begeistert.org> wrote:
I am trying to query a database using a SQL statement whose WHERE clause
is dependent on some variables. For example, if this condition is true, I would use this WHERE clause and use another WHERE clause if the condition is false (Actually it's not only the WHERE clause that is dynamic, I might query another table based on some other parameters). Previously I created a ZSQLMethod object with a lot of <dtml-if>s inside and it worked for me. However, I find modifying the object very confusing after a while. Basically my requirement is to build up a sql statement "on-the-fly" and, query the database with it and finally display the results

I found the following snippet of code in the "Zope Bible" which I think is a better solution compared to using alot of <dtml-if>s inside the ZSQLMethod object:

  from Products.ZSELMethods.SQL import SQL
  methodId = 'sqlListEmployees'
  setattr(self, methodId, SQL(methodId, '', 'employeesDB', 'Select * FROM     employees'))

  result =self.sqlListEmployees()

I think the book's code is based on Zope 2.4, but I am using Zope 2.6.1, and I changed the "self" key to "script".  

But I can't run the script as it failed at the first statement.

Any help or other suggestions is appreciated. I am still very new to Zope. :)

>On 2003-02-10 at 06:05:45 [+0100], eijgnit@netscape.net wrote:
>> Hi, I would like to use python to dynamically query a database.
>> import the SQL Method class is with:
>>     from Products.ZSQLMethods.SQL import SQL
>> Error Value: You are not allowed to access SQL in this context
>> How do I set the permissions such that I could import the SQL object? I 
>> don't have access to the LINUX system to modify the SQL.py file.
>Could you provide a bit more detail about what you're trying to do? It 
>sounds like you might be trying to use some Python module in a part of Zope 
>that doesn't allow it; that's why Zope's security doesn't let you. You 
>don't need to worry about the operating system permissions on the SQL.py 
>file. It's normally best to use a ZSQL-method do the work and call this 
>from a PageTemplate or a PythonScript. Importing the module directly is 
>more relevant for external methods, I think.
>Zope-DB mailing list

The NEW Netscape 7.0 browser is now available. Upgrade now! http://channels.netscape.com/ns/browsers/download.jsp 

Get your own FREE, personal Netscape Mail account today at http://webmail.netscape.com/