Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

checking to see if a database is empty

I want to create an option to delete a record in Sqlite, but I need to know if the database is empty. If it is empty then it gets stuck in the loop asking which record I want to delete. I tried

game_player = Player.select()
if game_player == None:
     print('no players found')

and did the requisite coding to get out of the loop. I suspect this is wrong. Any advice?

2 Answers

Good idea

Figured it out!!! To the people of the future, near and far:

game_players  =  Player.select()
if game_players.count() == 0:
    do the thing
else:
    do the other thing

count() gets the amount of records in the database.