Adam Azam
Adam Azam

Reputation: 103

How to count values in an SQLite table using Python?

I would like to know how to use the count function when using sqlite in python to look up and return the number of specific values there are in a table. For example, I want to be able to look in an sqlite table showing students and their characteristics and be able to find how many of them are male or female using python. I have done some research but have only found relevant information for when you are just using sqlite.

Upvotes: 2

Views: 13147

Answers (1)

Vikas Periyadath
Vikas Periyadath

Reputation: 3186

Did you try this :

 rowsQuery = "SELECT Count() FROM %s" % table
 cursor.execute(rowsQuery)
 numberOfRows = cursor.fetchone()[0]

you have to change the rowsQuery according to your need .

For your question:

 rowsQuery = "select count(STUDENT) from table_name where Gender= 'Male'"

Upvotes: 12

Related Questions