?
Solved

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

Posted on 2004-09-01
4
Medium Priority
?
1,690 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
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 …
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
Course of the Month12 days, left to enroll

752 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