Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cpython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sip
Cpython
Commits
f0bf7956
Unverified
Commit
f0bf7956
authored
Jul 29, 2022
by
Erlend Egeberg Aasland
Committed by
GitHub
Jul 29, 2022
Browse files
Options
Downloads
Patches
Plain Diff
gh-95273: Move sqlite3 executemany examples from reference to tutorial (#95351)
parent
e9c8de66
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
Doc/includes/sqlite3/executemany_1.py
+0
-26
0 additions, 26 deletions
Doc/includes/sqlite3/executemany_1.py
Doc/includes/sqlite3/executemany_2.py
+0
-17
0 additions, 17 deletions
Doc/includes/sqlite3/executemany_2.py
Doc/library/sqlite3.rst
+26
-11
26 additions, 11 deletions
Doc/library/sqlite3.rst
with
26 additions
and
54 deletions
Doc/includes/sqlite3/executemany_1.py
deleted
100644 → 0
+
0
−
26
View file @
e9c8de66
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
()
This diff is collapsed.
Click to expand it.
Doc/includes/sqlite3/executemany_2.py
deleted
100644 → 0
+
0
−
17
View file @
e9c8de66
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
()
This diff is collapsed.
Click to expand it.
Doc/library/sqlite3.rst
+
26
−
11
View file @
f0bf7956
...
@@ -67,15 +67,28 @@ after restarting the Python interpreter::
...
@@ -67,15 +67,28 @@ after restarting the Python interpreter::
con = sqlite3.connect('example.db')
con = sqlite3.connect('example.db')
cur = con.cursor()
cur = con.cursor()
To retrieve data after executing a SELECT statement, either treat the cursor as
At this point, our database only contains one row::
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.
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'):
>>> for row in cur.execute('SELECT * FROM stocks ORDER BY price'):
print(row)
...
print(row)
('2006-01-05', 'BUY', 'RHAT', 100, 35.14)
('2006-01-05', 'BUY', 'RHAT', 100, 35.14)
('2006-03-28', 'BUY', 'IBM', 1000, 45.0)
('2006-03-28', 'BUY', 'IBM', 1000, 45.0)
...
@@ -990,12 +1003,14 @@ Cursor Objects
...
@@ -990,12 +1003,14 @@ Cursor Objects
:term:`iterator` yielding parameters instead of a sequence.
:term:`iterator` yielding parameters instead of a sequence.
Uses the same implicit transaction handling as :meth:`~Cursor.execute`.
Uses the same implicit transaction handling as :meth:`~Cursor.execute`.
.. literalinclude:: ../includes/sqlite3/executemany_1.py
Example::
Here's a shorter example using a :term:`generator`:
.. literalinclude:: ../includes/sqlite3/executemany_2.py
data = [
("row1",),
("row2",),
]
# cur is an sqlite3.Cursor object
cur.executemany("insert into t values(?)", data)
.. method:: executescript(sql_script, /)
.. method:: executescript(sql_script, /)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment