Mailing List Archive

Re: RE : How lunch SQL request from python script ?
Please, stay on the list :)


First, isn't Charlie Clark solution good for you? It seems to be
reasonable.
> I have try to use external method but without success... (I have read some post as you suggested it)
>
Create database connection object called 'myconnection' in ZMI and
connect it to your database.
Create external method like:

def callSQL(self, statement):
conn = self.myconnection()
return conn.query(statement)

--
Maciej Wisniowski
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: RE : How lunch SQL request from python script ? [ In reply to ]
Am 19.01.2007, 12:22 Uhr, schrieb CERETTO Thierry <t.ceretto@chu-nancy.fr>:

Thierry,

keep replies on the mailing list so that others can learn from them.

> Hi,
> Thanks Charlie, your method works well (after some hours of labor...)
> My code below (not finalized, but, for Zope/Python/MySsql newbies, can
> be useful for to start...)

Yes, except that it's not really very Pythonic and we're the number of
script kiddies out there picking up the language we need to try and
encourage the good stuff.

> from Products.PythonScripts.standard import html_quote
> from string import split

No! strings have got the split() method (since Python 1.6)

> request = container.REQUEST
> RESPONSE = request.RESPONSE
> # Get argument(s)
> criteres = mot_MAJ
> # Test

Assuming the criteria are coming from some web form there should be no
need to split a string, they should already be available as a list.

> #for cc in criteres.split(' '):
> #print cc.upper()
> # Counter
> countResults = 0
> # Loop through arguments
> for c in criteres.split(' '):
> # Lecture_mots iS "Z SQL method" in Zope (ZMI) which use a "Z MySQL
> database connection"
> results = context.lecture_mots(critere='%'+c.upper()+'%')

I'd actually use upper() in the DB query but it doesn't make much
difference

> if len(results) == 0: # if not results is a nicer way to write this
> print 'No results for ' + c
> else:
> countResults = len(results)
> print str(countResults) + ' results for ' + c
> for ligne in results:
> print '<h3>' + ligne[1] + '</h3>'
> # Test
> #for colonne in ligne:
> #print colonne
> return results

Do not use a PythonScript to generate user readable output! Use it simply
to control your SQL query and pass the results to a template to display
which look something like this.
<tal:block tal:define="results context/myScript">

<h3 tal:repeat="ligne results" tal:content="ligne/id"></h3>
</tal:block>

As you actually wish to list the results by criteria then you should pass
the criteria to the script in a template loop but I'll leave that to you.

Charlie
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: RE : How lunch SQL request from python script ? [ In reply to ]
Am 19.01.2007, 13:27 Uhr, schrieb Maciej Wisniowski
<maciej.wisniowski@coig.katowice.pl>:

> Please, stay on the list
> First, isn't Charlie Clark solution good for you? It seems to be
> reasonable.

Thanks! ;-)

>> I have try to use external method but without success... (I have read
>> some post as you suggested it)
>>
> Create database connection object called 'myconnection' in ZMI and
> connect it to your database.
> Create external method like:
> def callSQL(self, statement):
> conn = self.myconnection()
> return conn.query(statement)

The only thing that's missing for me is using this idiom with existing
ZSQL methods and that's because I've forgotten how to get them to render
the query rather than execute it. Anyone care to enlighten me! ;-)

Charlie
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: RE : How lunch SQL request from python script ? [ In reply to ]
>
> The only thing that's missing for me is using this idiom with existing
> ZSQL methods and that's because I've forgotten how to get them to
> render the query rather than execute it. Anyone care to enlighten me! ;-)
Not sure if I understand you, but do you think about
src__=1 parameter when calling ZSQLMethod?

--
Maciej Wisniowski
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: RE : How lunch SQL request from python script ? [ In reply to ]
On Jan 19, 2007, at 7:31 AM, Charlie Clark wrote:

> keep replies on the mailing list so that others can learn from them.

<rant>

This is repeated so often that you'd think that the list
administrators would get a clue and set the default Reply-to: header
to include the list address. I run several technical lists, and
except on announcement lists, set the Reply-to: to the list address,
and these sorts of problems never happen. And despite the dire
warnings of the email purists, nothing bad has ever happened; the
world continues to turn just fine.

If it is important that replies go to the list, then the default
behavior should be just that. Send your complaints to the list
admins, not to the posters who expect things to work intelligently.

</rant>

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com


_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db