cpython/Doc/includes/sqlite3/execsql_fetchonerow.py

20 lines
554 B
Python
Raw Normal View History

2007-08-15 14:28:22 +00:00
import sqlite3
con = sqlite3.connect("mydb")
cur = con.cursor()
SELECT = "select name, first_appeared from people order by first_appeared, name"
2007-08-15 14:28:22 +00:00
# 1. Iterate over the rows available from the cursor, unpacking the
# resulting sequences to yield their elements (name, first_appeared):
2007-08-15 14:28:22 +00:00
cur.execute(SELECT)
for name, first_appeared in cur:
print(f"The {name} programming language appeared in {first_appeared}.")
2007-08-15 14:28:22 +00:00
# 2. Equivalently:
cur.execute(SELECT)
for row in cur:
print(f"The {row[0]} programming language appeared in {row[1]}.")
con.close()