[Tutor] Python + MySQL , my first simple program does not gives results
alan.gauld at freenet.co.uk
Thu Jan 19 20:36:41 CET 2006
> I was trying to get the some result , eg
> describe table, select * from table
> but my program does not give the expected results ,
> nor it gives any error
First of all you have only defined init() but not created an instance
so it never actually gets called! But if you do call it(by creating
an instance of learnpython_db)...
You are fetching the results into the cursor but you never access them!
So the second execute call will overwrite the first execute with the
SELECT results. But then you exit the init() function so cursor
(a local variable) is garbage collected and the results die with it..
> import MySQLdb
> class learnpython_db:
> def __init__(self):
> db =
> MySQLdb.connect(host="localhost",user =
> "john",password = "asdlkj", db = learnpython)
> cursor = db.cursor()
> cursor.execute(" describe contact" )
> cursor.execute(" SELECT * from
> contact" )
You probably want to make cursor (and maybe db too) an instance variable.
Then you can access the results of the cursor in other methods of the
You will see this illustrated in my tutorial topic albeit using SQLite but
cursor hjandling is nearly identical.
Author of the learn to program web tutor
More information about the Tutor