我正在尝试使用SQLAlchemy获得一个基本的应用程序,但遇到了一些麻烦。似乎我尝试保存到DB不起作用。这是我到目前为止所做的:
玩家类:
#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:
currentPlayer.rating = playerObject.rating
currentPlayer.ranking = playerObject.ranking
else:
addEntryToDB(playerObject, session)
def updateEntireDB(arrayOfPlayerObjects, session):
for player in arrayOfPlayerObjects:
updateSpecificDBEntry(player, session)
def findPlayerWithRanking(ranking, session):
currentPlayer = session.query(Player).filter(Player.ranking = ranking).first()
print(currentPlayer.firstName, currentPlayer.lastName, currentPlayer.rating, currentPlayer.ranking, currentPLayer.college)
某些特定于数据库的方法(保存在名为databaseFunctions.py的模块中):
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)
最后,这是我的主要方法:
findPlayerWithRanking()
理想情况下,这会设置我的数据库,用一堆玩家(~650)填充它,并打印出有关排名第100的玩家的信息。但是在我的None
函数中,我查询排名第100的玩家并返回2015-06-17 00:28:54,211 INFO sqlalchemy.engine.base.Engine ('Elizabeth H.', 'McDermott')
2015-06-17 00:28:54,212 INFO sqlalchemy.engine.base.Engine INSERT INTO players ("firstName", "lastName", college, ranking, rating) VALUES (?, ?, ?, ?, ?)
2015-06-17 00:28:54,212 INFO sqlalchemy.engine.base.Engine (659, 'Elizabeth H.', 'Denison', 'McDermott', 2.11)
2015-06-17 00:28:54,213 INFO sqlalchemy.engine.base.Engine COMMIT
2015-06-17 00:28:54,214 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2015-06-17 00:28:54,215 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.ranking = ?
LIMIT ? OFFSET ?
。
我的代码生成的SQL看起来有点粗略:实际正确插入:
def addEntryToDB(playerObject, session):
#create new player object
newPlayer = Player(playerObject.rating, playerObject.college, playerObject.ranking, playerObject.firstName, playerObject.lastName)
#add record to session object
session.add(newPlayer)
#commit record to database
session.commit()
第二行让我觉得它实际上并没有插入值(它只有“?”占位符。第三行显示正确的值,但它们似乎是乱序的。即在上面,第二行指定“ firstName“作为第一个字段,第三行中的值以659开头,她的排名。也许这是问题的一部分?
任何帮助都会很多赞赏, bclayman
编辑包含adEntryToDB功能:
playerObject
编辑添加打印 ranking is 658 firstName is Anisha lastName is Nawalrai Melwani rating is 2.28 college is Boston University
ranking is 659 firstName is Elizabeth H. lastName is McDermott rating is 2.12 college is Denison
的示例输出:
2015-06-17 14:03:46,298 INFO sqlalchemy.engine.base.Engine ('Elizabeth H.', 'McDermott', 1, 0)
2015-06-17 14:03:46,300 INFO sqlalchemy.engine.base.Engine INSERT INTO players ("firstName", "lastName", college, ranking, rating) VALUES (?, ?, ?, ?, ?)
2015-06-17 14:03:46,300 INFO sqlalchemy.engine.base.Engine ('Elizabeth H.', 'McDermott', 'Denison', 659, 2.12)
2015-06-17 14:03:46,300 INFO sqlalchemy.engine.base.Engine COMMIT
2015-06-17 14:03:46,302 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2015-06-17 14:03:46,303 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.ranking = ?
LIMIT ? OFFSET ?
编辑以包含新SQL:
{{1}}