我正在尝试将以下查询的结果存储到字典中。到目前为止,我已经设法通过执行以下操作将所选玩家的成本存储到列表中:
total = []
print("--------Midfielders--------")
for player in range(4):
name=(input("Enter Midfielders name: "))
role = "Midfielder"
cursor.execute('''SELECT player, position, cost
FROM players
WHERE player =? AND position =? ''',(name, role,))
print (cursor.fetchone())
cursor.execute('''SELECT player, position, cost
FROM players
WHERE player =? AND position =? ''',(name, role,))
total.append((cursor.fetchone()[2]))
print(sum(total))
print("-------Forwards--------")
for player in range(4):
name=(input("Enter Forwards name: "))
role = "Forward"
cursor.execute('''SELECT player, position, cost
FROM players
WHERE player =? AND position =? ''',(name, role,))
print (cursor.fetchone())
cursor.execute('''SELECT player, position, cost
FROM players
WHERE player =? AND position =? ''',(name, role,))
total.append((cursor.fetchone()[2]))
print(sum(total))
我希望能够将结果(播放器,位置,成本)存储到字典中。
通常我会设置字典
playerCost = {}
然后执行playerCost[player],[position]+=cost
之类的操作来创建显示Rooney Forward : 8.3
的字典。我尝试了各种排列,但我只是猜测如何用cursor.execute完成。非常感谢任何帮助。