Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm new to python and I'm trying to make this work. I'm using Python 2.7 and PostgreSQL 9.3:

#! F:\Python2.7.6\python    
import psycopg2

class Database:   
    host = "192.168.56.101"
    user = "testuser"
    passwd = "passwd"
    db = "test"

    def __init__(self):
        self.connection = psycopg2.connect( host = self.host,
                                            user = self.user,
                                            password = self.passwd,
                                            dbname = self.db )
        self.cursor = self.connection.cursor

    def query(self, q):
        cursor = self.cursor
        cursor.execute(q)
        return cursor.fetchall()

    def __del__(self):
        self.connection.close()

if __name__ == "__main__":
    db = Database()
    q = "DELETE FROM testschema.test"
    db.query(q)

However I am getting an error "AttributeError: 'builtin_function_or_method' object has no attribute 'execute'". I figure I should put something like self.execute = something in the Database class, but I can't figure it out what exactly I need to put there. Any suggestions?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You are missing the parenthesis at the end

self.cursor = self.connection.cursor()

or

cursor = self.cursor()

But not both

share|improve this answer
    
That did it. Thanks bro! –  Johann Mar 1 at 6:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.