1 | n/a | import sqlite3 |
---|
2 | n/a | |
---|
3 | n/a | con = sqlite3.connect("mydb") |
---|
4 | n/a | |
---|
5 | n/a | cur = con.cursor() |
---|
6 | n/a | SELECT = "select name_last, age from people order by age, name_last" |
---|
7 | n/a | |
---|
8 | n/a | # 1. Iterate over the rows available from the cursor, unpacking the |
---|
9 | n/a | # resulting sequences to yield their elements (name_last, age): |
---|
10 | n/a | cur.execute(SELECT) |
---|
11 | n/a | for (name_last, age) in cur: |
---|
12 | n/a | print('%s is %d years old.' % (name_last, age)) |
---|
13 | n/a | |
---|
14 | n/a | # 2. Equivalently: |
---|
15 | n/a | cur.execute(SELECT) |
---|
16 | n/a | for row in cur: |
---|
17 | n/a | print('%s is %d years old.' % (row[0], row[1])) |
---|