SQLite查询结果为dict

时间:2015-04-06 13:10:09

标签: python dictionary sqlite python-3.4

我正在尝试将以下查询的结果存储到字典中。到目前为止,我已经设法通过执行以下操作将所选玩家的成本存储到列表中:

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完成。非常感谢任何帮助。

0 个答案:

没有答案