Solved

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

Posted on 2004-09-01
4
1,610 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

Title # Comments Views Activity
Copy Files - Python 7 136
regex to extract ip:john 17 88
import 13 row from .csv file into another csv file 11 71
python - user input - set 4 79
Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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 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…

732 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