0

私はPythonでデータベースを練習し、Phpadminデータベースを使用してテーブルを作成しましたが、db.query(q)の34行目で、エラーは次のとおりです:AttributeError:データベースインスタンスに属性「クエリ」がありません

#!/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