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