Mercurial > cpython-lru_cache-weakref
annotate Doc/lib/sqlite3/simple_tableprinter.py @ 39496:352ffe77b71d legacy-trunk
Patch #1535500: fix segfault in BZ2File.writelines and make sure it
raises the correct exceptions.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Mon, 14 Aug 2006 21:42:55 +0000 |
| parents | 204ec383b445 |
| children |
| rev | line source |
|---|---|
|
38197
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
1 import sqlite3 |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
2 |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
3 FIELD_MAX_WIDTH = 20 |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
4 TABLE_NAME = 'people' |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
5 SELECT = 'select * from %s order by age, name_last' % TABLE_NAME |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
6 |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
7 con = sqlite3.connect("mydb") |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
8 |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
9 cur = con.cursor() |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
10 cur.execute(SELECT) |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
11 |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
12 # Print a header. |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
13 for fieldDesc in cur.description: |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
14 print fieldDesc[0].ljust(FIELD_MAX_WIDTH) , |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
15 print # Finish the header with a newline. |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
16 print '-' * 78 |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
17 |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
18 # For each row, print the value of each field left-justified within |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
19 # the maximum possible width of that field. |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
20 fieldIndices = range(len(cur.description)) |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
21 for row in cur: |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
22 for fieldIndex in fieldIndices: |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
23 fieldValue = str(row[fieldIndex]) |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
24 print fieldValue.ljust(FIELD_MAX_WIDTH) , |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
25 |
|
204ec383b445
Text files missing the SVN eol-style property.
Tim Peters <tim.peters@gmail.com>
parents:
38069
diff
changeset
|
26 print # Finish the row with a newline. |
