@@ -67,12 +67,25 @@ after restarting the Python interpreter::
67
67
con = sqlite3.connect('example.db')
68
68
cur = con.cursor()
69
69
70
- To retrieve data after executing a SELECT statement, either treat the cursor as
71
- an :term: `iterator `, call the cursor's :meth: `~Cursor.fetchone ` method to
72
- retrieve a single matching row, or call :meth: `~Cursor.fetchall ` to get a list
73
- of the matching rows.
70
+ At this point, our database only contains one row::
74
71
75
- This example uses the iterator form::
72
+ >>> res = cur.execute('SELECT count(rowid) FROM stocks')
73
+ >>> print(res.fetchone())
74
+ (1,)
75
+
76
+ The result is a one-item :class: `tuple `;
77
+ one row, with one column.
78
+ Now, let us insert three more rows of data,
79
+ using :meth: `~Cursor.executemany `::
80
+
81
+ >>> data = [
82
+ ('2006-03-28', 'BUY', 'IBM', 1000, 45.0),
83
+ ('2006-04-05', 'BUY', 'MSFT', 1000, 72.0),
84
+ ('2006-04-06', 'SELL', 'IBM', 500, 53.0),
85
+ ]
86
+ >>> cur.executemany('INSERT INTO stocks VALUES(?, ?, ?, ?)', data)
87
+
88
+ Now, retrieve the data by iterating over the result of a SELECT statement::
76
89
77
90
>>> for row in cur.execute('SELECT * FROM stocks ORDER BY price'):
78
91
print(row)
@@ -990,12 +1003,6 @@ Cursor Objects
990
1003
:term: `iterator ` yielding parameters instead of a sequence.
991
1004
Uses the same implicit transaction handling as :meth: `~Cursor.execute `.
992
1005
993
- .. literalinclude :: ../includes/sqlite3/executemany_1.py
994
-
995
- Here's a shorter example using a :term: `generator `:
996
-
997
- .. literalinclude :: ../includes/sqlite3/executemany_2.py
998
-
999
1006
1000
1007
.. method :: executescript(sql_script, /)
1001
1008
0 commit comments