[Zope] PYTHON script

Eric Walstad eric@walstads.net
Mon, 11 Jun 2001 15:40:04 -0700


Hey Norman,

This is kind of a stab in the dark, but it looks like your use of all single
quotes may be causing trouble.

If you want to use all single quotes (as in your email), you need to escape
your single quotes with backslashes:
myquery = 'SELECT location, SUM(IF(gender=\'M\',1,0)) AS M,
SUM(IF(gender=\'F\',1,0)) AS F\n'
myquery = myquery + 'FROM locations INNER JOIN employees USING (loc_code)
GROUP BY location;'

You can also define your string with triple quotes like:
myquery = """SELECT location, SUM(IF(gender='M',1,0)) AS M,
SUM(IF(gender='F',1,0)) AS F\n"""
myquery = myquery + "FROM locations INNER JOIN employees USING (loc_code)
GROUP BY location;"

Hope I'm on target with this one...

Eric.

> -----Original Message-----
> From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of
> Norman Khine
> Sent: Monday, June 11, 2001 3:20 PM
> To: Zope
> Subject: [Zope] PYTHON script
>
>
> Hello I have a MySQL database and I can run the following script using the
> command prompt
>
> SELECT location, SUM(IF(gender='M',1,0)) AS M,
> SUM(IF(gender='F',1,0)) AS F
> FROM locations INNER JOIN employees USING (loc_code) GROUP BY location;
>
> this also works within Zope
>
> but when I try to run this from the python prompt I get an
>
> File "<stdin>", line 1
>     b.execute('SELECT location, SUM(IF(gender='M',1,0)) AS M,
> SUM(IF(gender='F',1,0)) AS F FROM locations INNER JOIN employees USING
> (loc_code) GROUP BY location')
>                                                ^
> SyntaxError: invalid syntax
>
> this is what I did
>
> >>import MySQLdb
> >>db = MySQLdb.connect(db='mydb',user='myuser',passwd='mypasswd')
> >>b = db.cursor()
> >>b.execute('myquery')
>
> error
>
> any ideas
>
> thanks
>
> norman
>
> ps perhaps this post is more suited to the python mail list,
> please forgive
> as I am only a member of the zope community.;^)
>
> zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
> zz/********/z/****\zzz|****\*\zz|*******|z
> z/^^^^^^^^/z/******\zz|*^^^^|*|z|*|^^^^^|z norman khine
> zzzzzz/**/z|**/^^\**|z|*|zzz|*|z|*|zzzzzzz mailto:norman@khine.net
> zzzzz/**/zz|*|zzzz|*|z|****/*/zz|*****|zzz purley
> z/******/zz|*|zzzz|*|z|*|^^zzzzz|*|^^^|zzz UK
> zzZ/**/zzzz|**\^^/**|z|*|zzzzzzz|*|zzzzzzz
> zz/******/zz\******/zz|*|zzzzzzz|*|*****|z
> z/^^^^^^/zzzz\^^^^/zzz|^|zzzzzzz|^^^^^^^|z
> zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
>
>
>
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )