Solved

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

Posted on 2004-09-01
4
1,544 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
ID: 11951096
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
ID: 11951102
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
ID: 11957168
Thanks! The problem was because I didn't close the db connections!
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11961283
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

828 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