Solved

AttributeError: 'str' object has no attribute 'cursor'

Posted on 2004-09-01
4
1,420 Views
Last Modified: 2010-04-16
After the method getAuthorDetials() runs 10 times, I get the following error:
Traceback (most recent call last):
  File "make.py", line 421, in ?
  ... (no problem here)
  File "make.py", line 398, in makeCategoryArticles
    f = f + '<h4>Written by '+ article.getAuthorDetails()[0] + '</h4>'
  File "make.py", line 69, in getAuthorDetails
    acursor = db.cursor()
AttributeError: 'str' object has no attribute 'cursor'

**** What's going wrong? How do I fix it?

def getAuthorDetails(self):
        db = connectToDb()
        acursor = db.cursor()
        acursor.execute("select * from authors where author_id='"+
                       str(self.author_id) + "\'")
        # there should only be one row, so no need to worry about looping      
        row = acursor.fetchone()
        first_name    = row[1]
        last_name     = row[2]
        fullname      = first_name +" "+ last_name
        position      = row[3]
        email         = row[4]
        passwd        = row[5]
        article_count = row[6]
        biography     = row[7]
        portrait      = row[8]
        # return tuple of details                                              
        return (fullname, position, email, passwd, article_count, biography,
                portrait,)


def connectToDb(host="localhost", user="user", passwd="pass",
                    dbname="dbn"):
        try:
            thedb = MySQLdb.connect(host, user, passwd, dbname)
            return thedb
        except:
            print "custom db ERROR message"
            return "ERROR"
0
Comment
Question by:Ultrakapy
  • 3
4 Comments
 
LVL 11

Expert Comment

by:cjjclifford
Comment Utility
it could be that "connectToDb()" is returning "ERROR", which is not checked for in the calling loop - rather than returning "ERROR" raise an exception, or swallow the exception and "return None", checking the return value before using the DB Connection.

basically, connectToDb() can be replaced with:

def connectToDb(host="localhost", user="user", passwd="pass",
                    dbname="dbn"):
    return MySQLdb.connect( host, user, passwd, dbname )

and its usage then becomes:

def getAuthorDetails(self):
        try:
            db = connectToDb()
        except:
            print 'Failed to connect to DB....'
            return None

        acursor = db.cursor()
        acursor.execute("select * from authors where author_id='"+
                       str(self.author_id) + "\'")
        # there should only be one row, so no need to worry about looping      
        row = acursor.fetchone()
        first_name    = row[1]
        last_name     = row[2]
        fullname      = first_name +" "+ last_name
        position      = row[3]
        email         = row[4]
        passwd        = row[5]
        article_count = row[6]
        biography     = row[7]
        portrait      = row[8]
        # return tuple of details                                              
        return (fullname, position, email, passwd, article_count, biography,
                portrait,)
0
 
LVL 11

Accepted Solution

by:
cjjclifford earned 300 total points
Comment Utility
btw, take care, the DB connections/cursors are not being closed in the code you posted... (wrap the work with a try...finally, and close the connection in the finally block)
0
 

Author Comment

by:Ultrakapy
Comment Utility
Thanks! The problem was because I didn't close the db connections!
0
 
LVL 11

Expert Comment

by:cjjclifford
Comment Utility
thanks for the points - I'd say the two are related, the exception was getting raised after a while as connections were not being closed, your application ran out of connections, thus raising the exception, thus returning the string, which does not have the "cursor" attribute...
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now