PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/Doc/includes/sqlite3/simple_tableprinter.py

http://unladen-swallow.googlecode.com/
Python | 26 lines | 17 code | 6 blank | 3 comment | 6 complexity | 3007cb9f0958b7bc9e7ea3b2bd31d842 MD5 | raw file
 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) ,
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) ,
25
26    print # Finish the row with a newline.