简体   繁体   English

AttributeError: 类型对象“数据库”没有属性“scoreDB”

[英]AttributeError: type object 'Database' has no attribute 'scoreDB'

I'm trying to connect to Python using AWS RDS during the process of making games using pygame and open source.在使用 pygame 和开源制作游戏的过程中,我尝试使用 AWS RDS 连接到 Python。 When I run it, it says "AttributeError: type object 'Database' has no attribute 'scoreDB' "当我运行它时,它说“AttributeError: type object 'Database' has no attribute 'scoreDB'”

shooting_game.py射击游戏.py

import pygame
from database import Database

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

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

database.py数据库.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()


ERROR message错误信息

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

I tried to find the reason and implement it as it is, but I couldn't fix the error.我试图找到原因并按原样实施,但我无法修复错误。 I would appreciate it if you could help me with a beginner developer.如果您能帮助我初学者开发人员,我将不胜感激。

问题是你使用了装饰器@classmethod,当你使用这个时,你是在说这是一个静态方法,并且你正在尝试访问一个对象方法解决方案:删除getScores(self)方法的装饰器@classmethod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM