William Strauch
William Strauch

Reputation: 11

How to use SELECT COUNT sql to generate an int

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?

Upvotes: 1

Views: 862

Answers (1)

tadman
tadman

Reputation: 211590

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"]):
   ...

Upvotes: 1

Related Questions