0

I made a SQLite query in Python like that:

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")

It prints:

[{'SUM(Shares)':10}]

Then it gives me this error:

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

How can I extract the number as an integer?

0

2 Answers 2

2

It looks like SharesOwned is a list of dictionaries. To return the number of shares you need to do the following in this particular case:

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

SharesOwned[0] accesses the first element of the list, in this case a dictionary {'SUM(Shares)':10}. Now you just need to lookup the value by key - 'SUM(Shares)' in this case.

Additionally, it looks like you have a typo in sharesOwned, s should be capitalized.

Sign up to request clarification or add additional context in comments.

Comments

1

Try below changes : Update your query:

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

Then access SharedOwned:

if SharedOwned[0]['total']<sharesToSell:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.