In [3]:
import sqlite3
my_conn = sqlite3.connect('my_db.db')
print("Connected to database successfully")
Connected to database successfully
In [6]:
q="SELECT COUNT(DISTINCT(class)) FROM  student"
try:
    my_cursor=my_conn.execute(q)
    
    for row in my_cursor:
        print(row[0])
except sqlite3.Error as my_error:
  print("error: ",my_error)
7
In [9]:
q="SELECT DISTINCT class,sex FROM  student"
try:
    my_cursor=my_conn.execute(q)
    
    for row in my_cursor:
        print(row[0],row[1])
except sqlite3.Error as my_error:
  print("error: ",my_error)
Four female
Three male
Four male
Five male
Six male
Six female
Seven female
Nine male
Nine female
Eight male
Seven male
Three female
In [6]:
q="SELECT DISTINCT class FROM  student WHERE class NOT NULL"
try:
    my_cursor=my_conn.execute(q)
    
    for row in my_cursor:
        print(row[0])
except sqlite3.Error as my_error:
  print("error: ",my_error)
Four
Three
Five
Six
Seven
Nine
Eight
In [ ]: