1import sqlite3
2
3FIELD_MAX_WIDTH = 20
4TABLE_NAME = 'people'
5SELECT = 'select * from %s order by age, name_last' % TABLE_NAME
6
7con = sqlite3.connect("mydb")
8
9cur = con.cursor()
10cur.execute(SELECT)
11
12# Print a header.
13for fieldDesc in cur.description:
14    print(fieldDesc[0].ljust(FIELD_MAX_WIDTH), end=' ')
15print() # Finish the header with a newline.
16print('-' * 78)
17
18# For each row, print the value of each field left-justified within
19# the maximum possible width of that field.
20fieldIndices = range(len(cur.description))
21for row in cur:
22    for fieldIndex in fieldIndices:
23        fieldValue = str(row[fieldIndex])
24        print(fieldValue.ljust(FIELD_MAX_WIDTH), end=' ')
25
26    print() # Finish the row with a newline.
27
28con.close()
29