Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-09-01
4
Medium Priority
?
1,802 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 1200 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

963 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