Reputation: 9345
I'm writing a small Python script / app and am just getting my feet wet with SQLAlchemy. The only class I have is "Player", which is defined as follows:
class Player:
def __init__(self, rating, college, ranking = None, firstName = None, lastName = None):
self.rating = rating
self.college = college
self.ranking = ranking
self.firstName = firstName
self.lastName = lastName
Now I have a module called databaseFunctions.py and within it I have these two functions:
#if player exists, update his rating and ranking; otherwise, add new object to DB
def updateSpecificDBEntry(playerObject, session):
#find player in DB with firstName and lastName
currentPlayer = session.query(Player).filter(firstName == playerObject.firstName).filter(playerObject.lastName == lastName).first()
#set rating = newRating
if currentPlayer.count():
currentPlayer.rating = playerObject.rating
currentPlayer.ranking = playerObject.ranking
else:
addEntryToDB(playerObject, session)
def updateEntireDB(arrayOfPlayerObjects, session):
for player in arrayOfPlayerObjects:
updateSpecificDBEntry(player, session)
Here's what my main method looks like:
def main():
#create engine (for use by base and session)
engine = databaseFunctions.createEngine()
#create session
session = databaseFunctions.createSession(engine)
#get all players from CSA website
allPlayers = getSortedGender(constants.mainURL, constants.menTeams)
databaseFunctions.updateEntireDB(allPlayers, session)
My code actually works up until the last function call. At this point, I get the following error:
File "helpfulFunctions.py", line 114, in <module>
main()
File "helpfulFunctions.py", line 107, in main
databaseFunctions.updateEntireDB(allPlayers, session)
File "/Users/benjaminclayman/Desktop/SquashScraper/databaseFunctions.py", line 42, in updateEntireDB
updateSpecificDBEntry(player, session)
File "/Users/benjaminclayman/Desktop/SquashScraper/databaseFunctions.py", line 32, in updateSpecificDBEntry
currentPlayer = session.query(Player).filter(firstName == playerObject.firstName).filter(playerObject.lastName == lastName).first()
NameError: global name 'firstName' is not defined
I'm a bit confused because I'm querying all Player objects in the DB for entries whose first names match the firstName
of the passed in playerObject
and whose last names match the lastName
of the passed in playerObject
.
Any idea why I'm getting this error?
I initially called this function in main() but wasn't using it so took it out:
def createBase(engine):
Base = declarative_base()
Base.metadata.create_all(engine)
return Base
As in, I was saying:
Base = databaseFunctions.createBase(engine)
And then doing nothing with Base
. Not sure if this is part of the issue.
Thanks for the help, bclayman
Upvotes: 2
Views: 111
Reputation: 1846
In your updateSpecificDBEntry filter, you have:
.filter(firstName == playerObject.firstName)
The variable "firstName" doesn't exist.
It looks like you meant something like:
currentPlayer = session.query(Player).filter(Player.firstName == playerObject.firstName).filter(playerObject.lastName == Player.lastName).first()
So you're filtering a Player column, against the given playerObject attribute.
Upvotes: 2