[Tutor] Exceptions while dealing with MySQL

Thanos Panousis pthanos at gmail.com
Tue Apr 24 20:15:46 CEST 2007


I checked the SQLAlchemy and SQLObject projects, but they are not
really relevant to what I am doing(moreover they are more than I can
chew just yet:).

I managed to get a class variable to hold a cursor via something like

class person:
    cursor = MySQLdb.connect(stuff).cursor()

BUT when I make a function inside my class called myConnect, where I
do error checking and so on, I can't make it work:

class person:
    cursor = myConnect()

    __init(self)___:
        more things here....

   myConnect(self):
       try:
             return MySQLdb.connect(stuff).cursor()
       catch:
              print "Error!"

When trying to run this I get NameError: name 'myConnect' is not
defined. Any pointers for my OO-blindness?

Thanks a lot...


On 4/24/07, Kent Johnson <kent37 at tds.net> wrote:
> Thanos Panousis wrote:
> > I have an object, person, which is assosiated with some statistical
> > data. Say for each person object, I need an object variable called
> > "hairColor". This haircolor variable has to be filled through an SQL
> > query, so the object must have some way to access a database cursor.
> > The cool thing would be that all person objects use the same
> > connection/cursor to get their haircolors filled in, so there would be
> > only one connection to the SQL server.
> >
> > Could this  done with a class variable? From what I understand, it
> > fits nice because its available to all objects of the class, and it is
> > unique. So would this be a nice way to have objects of the same class
> > share a "pipe" to the sql database?
>
> Perhaps you should look into object-relational mappers such as SQLObject
> or SQLAlchemy.
>
> Kent
>


More information about the Tutor mailing list