Search code examples
pythonamazon-rdspymysql

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. When I run it, it says "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()


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.


Solution

  • the problem is that you used the decorator @classmethod, when you use this, your are saying that this is a static method, and you are tryng to acess a object method Solution: remove the decorator @classmethod of getScores(self) method