6

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?

1 Answer 1

7

You are missing the parenthesis at the end

self.cursor = self.connection.cursor()

or

cursor = self.cursor()

But not both

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

Comments

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.