2

I would like to write a class that would contain all my MySQL operations.

Right now, I cant even get the class instantiated.

Traceback (most recent call last):
  File "./compare.py", line 71, in <module>
    main()
  File "./compare.py", line 67, in main
    db = Table.mysqlconnect()
TypeError: unbound method mysqlconnect() must be called with Table instance as first argument (got nothing instead)

code:

import MySQLdb

class Table(object):
    """ Using Databases """
    def __init__(self, db, name ):
        self.db = db
        self.name = name
        self.cur = self.db.cursor()

    def mysqlconnect():
        conn = MySQLdb.connect (host = "mysql.blah.com",
            user = "user",
            passwd = "password",
            db = "database")
        cursor = conn.cursor ()
        cursor.execute ("SELECT VERSION()")
        row = cursor.fetchone ()
        print "server version:", row[0]
        cursor.close ()
        conn.close ()



def main():

    db = Table.mysqlconnect()
    pass

if __name__ == '__main__':
    main()
2
  • 1
    read section 9.3.2 "Class Objects" in @Christian's first link ( it's slightly above the linked bit) . This should give a good, generic, introduction. This link is a bit more advanced but should, hopefully, be of help as well. Commented Jan 1, 2012 at 16:00
  • thanks guys! will keep on reading Commented Jan 1, 2012 at 16:03

3 Answers 3

3

You should read those docs, but what you're looking for is:

db = Table()
db.mysqlconnect()

Short explanation: mysqlconnect is a instance method on your Table class.

Long explanation: Table is an abstract concept right now -- you've told the Python interpreter about it and what it should do, but you haven't actually made one yet. It's like the blueprint, if you will, for your class. Before you use it, or use any method defined as part of it* you'll need to actually "build it" first.

This is what you do when you do: db = Table() This tells the Python interpreter that I've got this variable called db now and I want it to be an instance of Table(). Now that you've got your instance, you can call the instance method (since the instance method only works on an instance) and get your result.

*There are things called class methods that you can use without instantiating the class first, but you'll see that when you read the documentation.

Sign up to request clarification or add additional context in comments.

Comments

3

The error is exactly right:

TypeError: unbound method mysqlconnect() must be called with Table instance as first argument (got nothing instead)

In other words, you are using a class name (Table) and not an instance of the class Table.

Read these examples, they do exactly what you want. If you want to read up on classes and objects, the Python manual chapter on this is excellent.

1 Comment

uhm, the article you linked does not quite describe how to use classes and MySQL... I think my problem is not understanding classes, not mysql function itself... will read on :) Thanks for the link
0

Also, mysqlconnect needs to use self to access the class attributes. For example:

import MySQLdb
class Table:
    def __init__(self, host, user, passwd, name)
        self.db = MySQLdb.connect (host = host,
            user = user,
            passwd = passwd,
            db = name)
        self.cursor = self.db.cursor()
    def mysqlconnect(self):
        self.cursor.execute ("SELECT VERSION()")
        row = cursor.fetchone()
        print "server version:", row[0]
        self.cursor.close ()
        self.db.close ()

# Main must be outside the table class
def main():
    tableInstance = Table("mysql.blah.com", "user", "password", "database")
    tableInstance.mysqlconnect()

if __name__ == '__main__':
    main()

3 Comments

I edited your post to show you that the main() function must be outside the scope of the Table() class. It's in the moderation queue right now though. If you make it part of the Table() class you don't have access to it by calling main() (much like how you can't just call mysqlconnect); you'd need a Table instance first.
Thanks. The identation system here has really been throwing me off.
I find it easiest to write my code in a text editor, copy and paste and then highlight and hit the {} button to convert it to code formatting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.