PSP class import + MySQL connect
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 i开发者_如何学运维s 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?
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.
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.fetchall():
since cursors are iterators.
精彩评论