0

I am practicing database in python and using Phpadmin database, and made my table but, In line 34 in db.query(q), error is : AttributeError: Database instance has no attribute 'query'

#!/user/env
import MySQLdb


class Database:

    host   = "localhost"
    user   = "root"
    passwd = "root"
    db     = "test"

    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 testtablee"

    db.query(q)

    q = """
    INSER INTO testtablee
    ('name', 'age')
    VALUES
    ('Mike', 39),
    ('Michael', 21),
    ('Angela', 21)
    """

    db.query(q)

    q = """
    SELECT * FROM testtablee
    WHERE age = 21
    """

    people = db.query(q)

    for person in people:
        print "Found: %s "% person['name']
4

1 回答 1

0

检查你的压痕。他们不一样。我认为这可能是您问题的答案。

于 2013-07-20T17:51:56.403 回答