###################################################################
# SQLITE - Select with a join
###################################################################
import sqlite3

conn  = sqlite3.connect('/home/sultans/data/sqlite/sqlite.db')      #server database
#conn = sqlite3.connect('/sqlite/sqlite.db')                        #local database 

conn.row_factory = sqlite3.Row                          #return a row dictionary (with col names/values) 
                                                        #instead of row tuple (with col values only)
curs = conn.cursor()

sql = '''
    SELECT * 
    FROM student join class on ssn=stu_ssn
    ORDER BY student_id
'''
curs.execute(sql)                                       #execute SQL

result = curs.fetchall()                                #get the query result

### print column headers #########################################
col_names = result[0].keys()                            #get the column headers from 1st row 
for name in col_names:                                  #loop through column headers
    print('%-12s' % name.upper(), end='')               #print each 12char long, left justified
print()

### print column values #########################################
for row in result:                                      #loop thru all rows of query result
    for col in row:                                     #loop thru all columns
        print('%-12s' % col, end='')
    print()
    
curs.close()
conn.close()