views:

24

answers:

2

Ok so im trying to import a class i made which connects to a MySQL database the class code is shown below:

class connection
    def__init__( self ):
        self.cnx = MySQLdb.connect(user='xxx',host='xxx',passwd='xxx',db='xxx')

All of the parameters for the mysql connection are correct and file containg the class is in the same directory as the PSP file. The class file is called cnx_class.py

when i run my PSP file i get 'cnx' isnt defined. My psp code is below:

<psp:file>
import cnx_class
</psp:file>
<%
cur = cnx.cursor()
cur.execute('select * from protein;')
rows = cur.fetchall()
for row in rows:
    req.write`(row)`
#end
%>

any help?

A: 

You are horribly, horribly confused as to how modules and classes work. Please read and work through at least the modules section and the classes section of the Python tutorial.

Ignacio Vazquez-Abrams
A: 

Try replacing

cur = cnx.cursor()

with

con=cnx_class.connection()
cur=con.cnx.cursor()

You can also replace

rows = cur.fetchall()
for row in rows:

with

for row in cur:

since cursors are iterators.

unutbu