mohammad
mohammad

Reputation: 2212

python error in main method defenetion

this my first python code , i am trying to connect to data base , i think the connection part was processed successfully

import MySQLdb

class Database:
        host = "localhost"
        user = "root"
        passwd = "root"
        db = "pitest"

        def __init__(self):
                self.connection = MySQLdb.connect( host = self.host,
                                                   user = self.user,
                                                   passwd = self.passwd,
                                                   db = self.db)
        def query(self, q):
                cursor = self.connection.cursor( MySQLdb.cursors.DictCursor )
                cursor.execute(q)

                return cursor.fetchall()

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




         if __name__ == "__main__":
                db = Database()

                q = "DELETE FROM users"
                db.query(q)

                q = """
                INSERT INTO users (title, fname, sname, age, email)
                VALUES ('A', 'Z', 'BIG Z', '20', '[email protected]'),
               ('B', 'X', 'BIG X', '30', '[email protected]'),
               ('C', 'Y', 'BIG Y', '24', '[email protected]'),
               ('A', 'W', 'BIG W', '29', '[email protected]')
                """

Every time i try to run this code i get this error:

  File "mysqlConnection.py", line 26
    if __name__ == "__main__":
                             ^

can any one help me please

Upvotes: 0

Views: 56

Answers (1)

Bidhan
Bidhan

Reputation: 10697

It is an indentation error. Your

if __name__ == '__main__':

is not in the same indentation level as

def __del__(self):

Please fix it. I think hitting backspace once before your if block will do.

Upvotes: 2

Related Questions