@@ -67,15 +67,28 @@ 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
+ Then, 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
- print(row)
91
+ ... print(row)
79
92
80
93
('2006-01-05', 'BUY', 'RHAT', 100, 35.14)
81
94
('2006-03-28', 'BUY', 'IBM', 1000, 45.0)
@@ -990,12 +1003,14 @@ 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
1006
+ Example::
998
1007
1008
+ data = [
1009
+ ("row1",),
1010
+ ("row2",),
1011
+ ]
1012
+ # cur is an sqlite3.Cursor object
1013
+ cur.executemany("insert into t values(?)", data)
999
1014
1000
1015
.. method :: executescript(sql_script, /)
1001
1016
0 commit comments