我尝试使用SQLAlchemy在Python中设置我的数据库。我有一个像这样的Player类:
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
然后我在一个名为databaseFunctions.py的模块中有一些特定于数据库的方法:
#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)
我的主要方法非常基础:
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)
databaseFunctions.findPlayerWithRanking(100, session)
几个问题:
1)我在最后一次函数调用时遇到错误。这是错误消息:
File "helpfulFunctions.py", line 116, in <module>
main()
File "helpfulFunctions.py", line 108, 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 34, in updateSpecificDBEntry
if currentPlayer.count():
AttributeError: 'NoneType' object has no attribute 'count'
SQLAlchemy不喜欢我使用if currentPlayer.count()
,其中currentPlayer
是从我的SQLAlchemy查询返回的对象。我很困惑,因为我查看了文档,而SQLAlchemy 有count()
方法:
2)我试图在我的DB中找到排名为100的玩家对象。但看起来我的查询返回None对象(如currentPlayer
),这是非常令人惊讶的。我显然做错了什么。
我基本上刮掉所有玩家,将它们变成玩家对象,对它们进行排序,然后循环并给出正确的排名。然后我使用以下方法将所有这些添加到数据库中。所以 应该是数据库中排名为100的玩家(有大约650名玩家被刮掉)。
知道这里发生了什么吗?
感谢您的帮助, bclayman
编辑以显示由我的代码生成的SQL示例:
2015-06-16 20:05:33,664 INFO sqlalchemy.engine.base.Engine ('Lilliian A.', 'Wacker', 1, 0)
2015-06-16 20:05:33,666 INFO sqlalchemy.engine.base.Engine INSERT INTO players ("firstName", "lastName", college, ranking, rating) VALUES (?, ?, ?, ?, ?)
2015-06-16 20:05:33,666 INFO sqlalchemy.engine.base.Engine (652, 'Lilliian A.', 'Boston University', 'Wacker', 2.51)
2015-06-16 20:05:33,667 INFO sqlalchemy.engine.base.Engine COMMIT
2015-06-16 20:05:33,669 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2015-06-16 20:05:33,670 INFO sqlalchemy.engine.base.Engine SELECT players.id AS players_id, players."firstName" AS "players_firstName", players."lastName" AS "players_lastName", players.college AS players_college, players.ranking AS players_ranking, players.rating AS players_rating
FROM players
WHERE players."firstName" = ? AND players."lastName" = ?
LIMIT ? OFFSET ?
看起来像这部分:
2015-06-16 20:05:33,666 INFO sqlalchemy.engine.base.Engine INSERT INTO玩家(&#34; firstName&#34;,&#34; lastName&#34;,大学,排名,评级)VALUES (?,?,?,?,?)
可能没有在玩家表中插入任何内容...
想法?