raspberry pi - python class definition problrm -


this first python code trying connect database , defined class called database in python file called mysqlconnection.py

but when run code error :

traceback (most recent call last):   file "mysqlconnection.py", line 3, in <module>     class database:   file "mysqlconnection.py", line 27, in database     db = database() 

my code :

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 users"                 db.query(q)                  q = """                 insert users (title, fname, sname, age, email)                 values ('a', 'z', 'big z', '20', 'raspberrypi@example.com'),                ('b', 'x', 'big x', '30', 'raspberrypi@example.com'),                ('c', 'y', 'big y', '24', 'raspberrypi@example.com'),                ('a', 'w', 'big w', '29', 'raspberrypi@example.com')                 """                 db.query(q)                 q = """                 select * users title = 'a'                 """                  allreturnedusers = db.query(q)                  user in allreturnedusers:                         print "found: %s " % user['fname'] 

can 1 me please ?

your code improperly indented starting @ line

if __name__ == '__main__': 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -