Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count values in an SQLite table using Python?

Tags:

python

sqlite

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.

like image 542
Adam Azam Avatar asked Sep 06 '26 05:09

Adam Azam


1 Answers

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'"
like image 149
Vikas Periyadath Avatar answered Sep 07 '26 17:09

Vikas Periyadath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!