AttributeError: type object 'Database' has no attribute 'scoreDB'

AttributeError: type object 'Database' has no attribute 'scoreDB'

我在使用 pygame 和开源制作游戏的过程中尝试使用 AWS RDS 连接到 Python。当我 运行 它时,它说“AttributeError: type object 'Database' has no attribute 'scoreDB' “

shooting_game.py

import pygame
from database import Database

def main():
    hiScores=Database().getScores()

if __name__ == '__main__':
    while(main()):
        pass

database.py


import pymysql

class Database(object):
    numScores = 15
    def __init__(self,host='database-1.c79ahye2go7m.ap-northeast-2.rds.amazonaws.com',user='admin',password='letskirin',db='hiScores',charset='utf8'):
        self.scoreDB=pymysql.connect(host=host,user=user,password=password,db=db,charset=charset)
        self.cursor=self.scoreDB.cursor(pymysql.cursors.DictCursor)
    @staticmethod
    def getSound(self,music=False):
        curs = self.scoreDB.cursor(pymysql.cursors.DictCursor)
        if music:
            curs.execute("CREATE TABLE if not exists music (setting integer)")
            curs.execute("SELECT * FROM music")
        else:
            curs.execute("CREATE TABLE if not exists sound (setting integer)")
            curs.execute("SELECT * FROM sound")
        self.scoreDB.commit()
        setting = curs.fetchall()
        curs.close()
        return bool(setting[0][0]) if len(setting) > 0 else False

    @staticmethod
    def setSound(self,setting, music=False):
        curs = self.scoreDB.cursor()
        if music:
            curs.execute("DELETE FROM music")
            curs.execute("INSERT INTO music VALUES (?)", (setting,))
        else:
            curs.execute("DELETE FROM sound")
            curs.execute("INSERT INTO sound VALUES (?)", (setting,))
        self.scoreDB.commit()
        curs.close()

    @classmethod
    def getScores(self):
        curs = self.scoreDB.cursor()

        curs.execute('''CREATE TABLE if not exists scores
                     (name text, score integer, accuracy real)''')
        curs.execute("SELECT * FROM scores ORDER BY score DESC")
        self.scoreDB.commit()
        hiScores = curs.fetchall()
        curs.close()
        return hiScores

    @staticmethod
    def setScore(self,hiScores,entry):
        curs = self.scoreDB.cursor()

        if len(hiScores) == Database.numScores:
            lowScoreName = hiScores[-1][0]
            lowScore = hiScores[-1][1]
            curs.execute("DELETE FROM scores WHERE (name = ? AND score = ?)",
                      (lowScoreName, lowScore))
        curs.execute("INSERT INTO scores VALUES (?,?,?)", entry)
        self.scoreDB.commit()
        curs.close()


错误信息

Traceback (most recent call last):
  File "c:\Users\giuni27\ossp\Base_shooting-game\shooting_game.py", line 683, in <module>
    while(main()):
  File "c:\Users\giuni27\ossp\Base_shooting-game\shooting_game.py", line 127, in main
    hiScores=Database.getScores()
  File "c:\Users\giuni27\ossp\Base_shooting-game\database.py", line 43, in getScores
    curs = self.scoreDB.cursor()
AttributeError: type object 'Database' has no attribute 'scoreDB

我试图找到原因并按原样执行,但无法修复错误。如果您能帮助我开发初学者,我将不胜感激。

问题是你使用了装饰器@classmethod,当你使用这个时,你说这是一个静态方法,你试图访问一个对象方法 解决方法:去掉getScores(self)的装饰器@classmethod 方法