开发者

PSP class import + MySQL connect

开发者 https://www.devze.com 2022-12-22 05:42 出处:网络
Ok so im trying to import a class i made which connects to a MySQL database the class code is shown below:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消