1

I have this code in python querying an existing database:

count = db.execute("SELECT COUNT (player_id) FROM players WHERE user_id 
= :id", id = session["user_id"])

for player in range(count):
    db.execute("INSERT INTO live_players (player_id) VALUES (:filler)", 
filler = 0)

However, it doesn't run because 'count' isn't an int. I want SELECT COUNT to return 2, but instead it returns '[{'COUNT (player_id)': 2}]'. How could I remedy this?

1 Answer 1

1

You're getting back a result dictionary. Simply access it. To make this access easier, give it an alias:

SELECT COUNT(player_id) AS player_count ...

Then:

for player in range(count["player_count"]):
   ...
Sign up to request clarification or add additional context in comments.

3 Comments

I did this but got this error: list indices must be integers or slices, not str.
'count' now returns '[{'player_count': 2}]' instead of '[{'COUNT (player_id)': 2}]'. Also, I used alecxe's suggestion, but it still didn't work.
That should be a simple dict, so you should be able to fetch the value you want. Alexce was right, that was a typo. It could also be count[0]["player_count"] if that's a nested structure.

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.