使用Python从SQLite查询中提取整数值

2024-10-08 18:26:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python进行了一个SQLite查询,如下所示:

SharesOwned = db.execute("SELECT SUM(Shares) FROM transactionTable WHERE Share='FB'")

print(SharesOwned)
if sharesOwned < sharesToSell:
    return errorMessage("You don't own that many shares of FB")

它打印:

^{pr2}$

然后它给我这个错误:

TypeError: "List indices must be integers or slices not str"

如何提取整数形式的数字?在


Tags: fromshareexecutedbsqlitefbifwhere
2条回答

看起来sharesoowned是一个字典列表。在这种情况下,要返回股份数量,您需要执行以下操作:

SharesOwned[0]['SUM(Shares)'] 

sharesowed[0]访问列表的第一个元素,在本例中是字典{'SUM(Shares)':10}。现在您只需要按键查找值,在本例中是“SUM(Shares)”。在

另外,看起来你在sharesoowned中有一个错误,s应该大写。在

尝试以下更改: 更新您的查询:

SELECT SUM(Shares) AS total FROM transactionTable WHERE Share='FB'

然后共享访问:

^{pr2}$

相关问题 更多 >

    热门问题