Home | History | Annotate | Download | only in sqlite3
      1 import sqlite3
      2 
      3 FIELD_MAX_WIDTH = 20
      4 TABLE_NAME = 'people'
      5 SELECT = 'select * from %s order by age, name_last' % TABLE_NAME
      6 
      7 con = sqlite3.connect("mydb")
      8 
      9 cur = con.cursor()
     10 cur.execute(SELECT)
     11 
     12 # Print a header.
     13 for fieldDesc in cur.description:
     14     print fieldDesc[0].ljust(FIELD_MAX_WIDTH) ,
     15 print # Finish the header with a newline.
     16 print '-' * 78
     17 
     18 # For each row, print the value of each field left-justified within
     19 # the maximum possible width of that field.
     20 fieldIndices = range(len(cur.description))
     21 for 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.
     27