This is a first of a kind problem for me, and really not sure how to troubleshoot it.
I have a simple Python/MySQL project I'm working on for fun. I have a separate class, with it's own methods, to handle querying the database (using MySQLdb).
The method which seems to be the culprit (BlogDB.get_blogs()) returns the data fetched from the query. I then assign this to a variable in the main script (so I can send it to a parser and do everything else I need to with it). The problem is that as soon as I call that method (.get_blogs()) it outputs a field from the query results, even when I've got nothing set to output. I've tried without assigning it to a variable, just running the method, and it still does the same, and it's always the same field.
What am I missing, can someone please point me in the direction to see this? The method is so short I have no idea what's going on.
The main file, which calls everything else and ties it all together:
#!/usr/bin/python
print "Content-type: text/htmlnn"
import blog_db
bdb = blog_db.BlogDB()
bdb.get_blogs()
db.close_db()
Here's the blog_db module with the methods, though it's only the .get_blogs() method that causes problems (if I comment it out then the issue doesn't occur):
#!/usr/bin/python
import MySQLdb
class BlogDB():
def __init__(self):
db = MySQLdb.connect("mysql.server","user","pass","table" )
self.cursor = db.cursor()
def get_blogs(self):
self.cursor.execute("SELECT * from lt_blog ORDER BY id DESC LIMIT 5;")
data = self.cursor.fetchall()
return data
def close_db(self):
db.close()
Really at a loss on this one, hoping someone else has encountered something like this.
Using Python 2.7 and MySQL 5.3 and don't know which version of MySQLdb, Linux Debian with Apache web server.
[Edit] When running PDB in interactive mode I confirmed that the method call (.get_blogs()) is the culprit somehow, here is a screenshot from the PDB session. If there's anyway to delve deeper with PDB please let me know. The name (John Clark) isn't printed in the method (as you can see from the code above nothing in the method is set to output anything), and it's only in ONE of the rows returned, the 4th column in that row, so I really don't understand this or what's causing it.
Screen capture of live PDB session
Aucun commentaire:
Enregistrer un commentaire