source: python/trunk/Doc/includes/sqlite3/simple_tableprinter.py

Last change on this file was 2, checked in by Yuri Dario, 15 years ago

Initial import for vendor code.

  • Property svn:eol-style set to native
File size: 697 bytes
Line 
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.
Note: See TracBrowser for help on using the repository browser.