ยปCore Development>Code coverage>Doc/includes/sqlite3/simple_tableprinter.py

Python code coverage for Doc/includes/sqlite3/simple_tableprinter.py

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