[Python-checkins] gh-95273: Move sqlite3 executemany examples from reference to tutorial (GH-95351)
miss-islington
webhook-mailer at python.org
Fri Jul 29 03:51:25 EDT 2022
https://github.com/python/cpython/commit/f06f3656c5e98843d4666d22df0b2c5dd62c87ae
commit: f06f3656c5e98843d4666d22df0b2c5dd62c87ae
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-07-29T00:51:20-07:00
summary:
gh-95273: Move sqlite3 executemany examples from reference to tutorial (GH-95351)
(cherry picked from commit f0bf7956e60b452208f279146f928d71eb0aa11b)
Co-authored-by: Erlend Egeberg Aasland <erlend.aasland at innova.no>
files:
D Doc/includes/sqlite3/executemany_1.py
D Doc/includes/sqlite3/executemany_2.py
M Doc/library/sqlite3.rst
diff --git a/Doc/includes/sqlite3/executemany_1.py b/Doc/includes/sqlite3/executemany_1.py
deleted file mode 100644
index edf6f8b7ebe61..0000000000000
--- 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 02a594c861e15..0000000000000
--- 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 e6b0714f27055..d8ecc24d034b5 100644
--- a/Doc/library/sqlite3.rst
+++ b/Doc/library/sqlite3.rst
@@ -67,15 +67,28 @@ 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)
+
+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)
@@ -980,12 +993,14 @@ 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
+ Example::
+ data = [
+ ("row1",),
+ ("row2",),
+ ]
+ # cur is an sqlite3.Cursor object
+ cur.executemany("insert into t values(?)", data)
.. method:: executescript(sql_script, /)
More information about the Python-checkins
mailing list