1

I'm having trouble committing an insert statement.

It seems that python can't call the db object and I do not understand why.

Error:

On line self.db.commit()

[-] ERROR:  'NoneType' object has no attribute 'commit'

Code:

class Database(object):    
    def __init__(self):
        self.db = self.db_connect()

    def db_connect(self):
        try:
            self.db = MySQLdb.connect(host=MYSQL_HOST, 
                                    user=MYSQL_USER, 
                                    passwd=MYSQL_PWD, 
                                  db=MYSQL_DATABASE) 
            print '[+] Connected to {db} database'.format(db=MYSQL_DATABASE)
            self.cur = self.db.cursor()
        except Exception as e:
            print '[-] ERROR: ',e

    def add_email(self, email):
        try:            
            self.cur.execute('INSERT INTO User (email) VALUES (%s)', (email))           
            self.db.commit()
            print '[+] Added {email} to User(email) '.format(email=email)
        except Exception as e:
            print '[-] ERROR: ',e
            print '[*] Rolling back'
            self.db.rollback()

1 Answer 1

2

Your function def db_connect not return any thing. Means it returns None.

As per your code

def __init__(self):
    self.db = self.db_connect() # db_connect return None

set self.db as None and when you try to commit, it gives error.

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.