[Zope-DB] Python script in Zope

Charlie Clark charlie@begeistert.org
Tue, 03 Dec 2002 23:19:09 +0100


On 2002-12-03 at 23:00:21 [+0100], pouch@freemail.hu wrote:
> Hi!
> 
> I've got the following Python script:
> # Example code:

> 
> This works well on the Python interpreter console. 

hej FeR=F3!

you don't need to access the database like that in Zope. Just create a 
database connection object, create a ZSQL method which uses that object and=
 
call that method from a PythonScript, PageTemplate or DTML - try using a 
Z-SearchInterface to see how this works.

> db =3D MySQLdb.connect(host=3D"localhost", user=3D"root", passwd=3D"secre=
t", 
> db=3D"test")
use this in your connection object

> cursor =3D db.cursor()
don't need this

> cursor.execute("SELECT * FROM books")
Put SELECT * FROM books in your ZSQL method "qBooks" or similar

> result =3D cursor.fetchall()
> for record in result:
>    print record[0], "-->", record[1]

don't need the rest.

Skeleton PythonScript:

results =3D context.qBooks()
if results:
=09for result in results:
=09=09print result

return printed

But that is not what you want to do.

Skeleton ZPT

<html>
<head></head>
<body>
<p tal:repeat=3D"result here/qBooks" tal:content=3D"result/...">put the res=
ults 
of the query here but you need to know the names of the columns</p>
</body>
</html>

Charlie