From 6a5cb6105fac60e6904827f0eed66c11744bee1d Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 28 Jul 2022 00:41:00 +0200 Subject: [PATCH 1/6] gh-95273: Move sqlite3 executemany examples from reference to tutorial --- Doc/includes/sqlite3/executemany_1.py | 26 ------------------------ Doc/includes/sqlite3/executemany_2.py | 17 ---------------- Doc/library/sqlite3.rst | 29 +++++++++++++++++---------- 3 files changed, 18 insertions(+), 54 deletions(-) delete mode 100644 Doc/includes/sqlite3/executemany_1.py delete mode 100644 Doc/includes/sqlite3/executemany_2.py diff --git a/Doc/includes/sqlite3/executemany_1.py b/Doc/includes/sqlite3/executemany_1.py deleted file mode 100644 index edf6f8b7ebe61a..00000000000000 --- a/Doc/includes/sqlite3/executemany_1.py +++ /dev/null @@ -1,26 +0,0 @@ -import sqlite3 - -class IterChars: - def __init__(self): - self.count = ord('a') - - def __iter__(self): - return self - - def __next__(self): - if self.count > ord('z'): - raise StopIteration - self.count += 1 - return (chr(self.count - 1),) # this is a 1-tuple - -con = sqlite3.connect(":memory:") -cur = con.cursor() -cur.execute("create table characters(c)") - -theIter = IterChars() -cur.executemany("insert into characters(c) values (?)", theIter) - -cur.execute("select c from characters") -print(cur.fetchall()) - -con.close() diff --git a/Doc/includes/sqlite3/executemany_2.py b/Doc/includes/sqlite3/executemany_2.py deleted file mode 100644 index 02a594c861e15b..00000000000000 --- a/Doc/includes/sqlite3/executemany_2.py +++ /dev/null @@ -1,17 +0,0 @@ -import sqlite3 -import string - -def char_generator(): - for c in string.ascii_lowercase: - yield (c,) - -con = sqlite3.connect(":memory:") -cur = con.cursor() -cur.execute("create table characters(c)") - -cur.executemany("insert into characters(c) values (?)", char_generator()) - -cur.execute("select c from characters") -print(cur.fetchall()) - -con.close() diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index d493f5fd95631d..c9b53adecb64f7 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -67,12 +67,25 @@ after restarting the Python interpreter:: con = sqlite3.connect('example.db') cur = con.cursor() -To retrieve data after executing a SELECT statement, either treat the cursor as -an :term:`iterator`, call the cursor's :meth:`~Cursor.fetchone` method to -retrieve a single matching row, or call :meth:`~Cursor.fetchall` to get a list -of the matching rows. +At this point, our database only contains one row:: -This example uses the iterator form:: + >>> res = cur.execute('SELECT count(rowid) FROM stocks') + >>> print(res.fetchone()) + (1,) + +The result is a one-item :class:`tuple`; +one row, with one column. +Now, let us insert three more rows of data, +using :meth:`~Cursor.executemany`:: + + >>> data = [ + ('2006-03-28', 'BUY', 'IBM', 1000, 45.0), + ('2006-04-05', 'BUY', 'MSFT', 1000, 72.0), + ('2006-04-06', 'SELL', 'IBM', 500, 53.0), + ] + >>> cur.executemany('INSERT INTO stocks VALUES(?, ?, ?, ?)', data) + +Now, retrieve the data by iterating over the result of a SELECT statement:: >>> for row in cur.execute('SELECT * FROM stocks ORDER BY price'): print(row) @@ -990,12 +1003,6 @@ Cursor Objects :term:`iterator` yielding parameters instead of a sequence. Uses the same implicit transaction handling as :meth:`~Cursor.execute`. - .. literalinclude:: ../includes/sqlite3/executemany_1.py - - Here's a shorter example using a :term:`generator`: - - .. literalinclude:: ../includes/sqlite3/executemany_2.py - .. method:: executescript(sql_script, /) From a4f6dad24f9cdb2d09a838156179ed990ff70bef Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Thu, 28 Jul 2022 08:40:53 +0200 Subject: [PATCH 2/6] Update Doc/library/sqlite3.rst Co-authored-by: CAM Gerlach --- Doc/library/sqlite3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index c9b53adecb64f7..272b0ab4cf96af 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -73,7 +73,7 @@ At this point, our database only contains one row:: >>> print(res.fetchone()) (1,) -The result is a one-item :class:`tuple`; +The result is a one-item :class:`tuple`: one row, with one column. Now, let us insert three more rows of data, using :meth:`~Cursor.executemany`:: From 00a335ff8896e3e9d8bbfaf265369cadb8a39421 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Thu, 28 Jul 2022 08:40:58 +0200 Subject: [PATCH 3/6] Update Doc/library/sqlite3.rst Co-authored-by: CAM Gerlach --- Doc/library/sqlite3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index 272b0ab4cf96af..1ed5ce3838861d 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -85,7 +85,7 @@ using :meth:`~Cursor.executemany`:: ] >>> cur.executemany('INSERT INTO stocks VALUES(?, ?, ?, ?)', data) -Now, retrieve the data by iterating over the result of a SELECT statement:: +Then, retrieve the data by iterating over the result of a ``SELECT`` statement:: >>> for row in cur.execute('SELECT * FROM stocks ORDER BY price'): print(row) From c76b51d249c4711dc4b9462ac1ebfe118fbbe4c6 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 28 Jul 2022 15:14:48 +0200 Subject: [PATCH 4/6] Fix inlined code syntax --- Doc/library/sqlite3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index 1ed5ce3838861d..f99dad26be1a8b 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -88,7 +88,7 @@ using :meth:`~Cursor.executemany`:: Then, retrieve the data by iterating over the result of a ``SELECT`` statement:: >>> for row in cur.execute('SELECT * FROM stocks ORDER BY price'): - print(row) + ... print(row) ('2006-01-05', 'BUY', 'RHAT', 100, 35.14) ('2006-03-28', 'BUY', 'IBM', 1000, 45.0) From 36662994d842a79d293509265b89c9d5bb23121f Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 28 Jul 2022 15:38:56 +0200 Subject: [PATCH 5/6] Add small example --- Doc/library/sqlite3.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index f99dad26be1a8b..87e19a9b8bf638 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -1003,6 +1003,13 @@ Cursor Objects :term:`iterator` yielding parameters instead of a sequence. Uses the same implicit transaction handling as :meth:`~Cursor.execute`. + Example:: + + data = [ + ("row1",), + ("row2",), + ] + cur.executemany("insert into t values(?)", data) .. method:: executescript(sql_script, /) From 598d9d8b6851e621392722a5acfa212aeddc62a9 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Fri, 29 Jul 2022 09:27:18 +0200 Subject: [PATCH 6/6] Update Doc/library/sqlite3.rst --- Doc/library/sqlite3.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index 87e19a9b8bf638..fdc2727119eaf3 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -1009,6 +1009,7 @@ Cursor Objects ("row1",), ("row2",), ] + # cur is an sqlite3.Cursor object cur.executemany("insert into t values(?)", data) .. method:: executescript(sql_script, /)