Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-96121: Merge sqlite3.Row examples#96122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
erlend-aasland merged 6 commits into python:main from erlend-aasland:sqlite-docs/row-exampleAug 22, 2022
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits Select commit Hold shift + click to select a range
d2188bf Merge sqlite3.Row examples
erlend-aasland e1540d5 Remove the how-to and incorporate it into the reference
erlend-aasland 74c3a20 Address review
erlend-aasland 48dc7d9 Update Doc/library/sqlite3.rst
f83999f Update Doc/library/sqlite3.rst
6ea9b18 Update Doc/library/sqlite3.rst
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -207,7 +207,6 @@ inserted data and retrieved values from it in multiple ways. | ||
| * :ref:`sqlite3-placeholders` | ||
| * :ref:`sqlite3-adapters` | ||
| * :ref:`sqlite3-converters` | ||
| * :ref:`sqlite3-columns-by-name` | ||
| * :ref:`sqlite3-connection-context-manager` | ||
| * :ref:`sqlite3-explanation` for in-depth background on transaction control. | ||
| @@ -1255,17 +1254,21 @@ Cursor objects | ||
| >>> cur.connection == con | ||
| True | ||
| .. The sqlite3.Row example used to be a how-to. It has now been incorporated | ||
| into the Row reference. We keep the anchor here in order not to break | ||
| existing links. | ||
| .. _sqlite3-columns-by-name: | ||
| .. _sqlite3-row-objects: | ||
| Row objects | ||
| ^^^^^^^^^^^ | ||
| .. class:: Row | ||
| A :class:`Row` instance serves as a highly optimized | ||
| A :class:`!Row` instance serves as a highly optimized | ||
| :attr:`~Connection.row_factory` for :class:`Connection` objects. | ||
| It tries to mimic a :class:`tuple` in most of its features, | ||
| and supports iteration, :func:`repr`, equality testing, :func:`len`, | ||
| It supports iteration, equality testing, :func:`len`, | ||
| and :term:`mapping` access by column name and index. | ||
| Two row objects compare equal if have equal columns and equal members. | ||
| @@ -1279,45 +1282,18 @@ Row objects | ||
| .. versionchanged:: 3.5 | ||
| Added support of slicing. | ||
| Let's assume we initialize a table as in the example given above:: | ||
| Example:: | ||
| con = sqlite3.connect(":memory:") | ||
| cur = con.cursor() | ||
| cur.execute('''create table stocks | ||
| (date text, trans text, symbol text, | ||
| qty real, price real)''') | ||
| cur.execute("""insert into stocks | ||
| values ('2006-01-05','BUY','RHAT',100,35.14)""") | ||
| con.commit() | ||
| cur.close() | ||
| Now we plug :class:`Row` in:: | ||
| >>> con.row_factory = sqlite3.Row | ||
| >>> cur = con.cursor() | ||
| >>> cur.execute('select * from stocks') | ||
| <sqlite3.Cursor object at 0x7f4e7dd8fa80> | ||
| >>> r = cur.fetchone() | ||
| >>> type(r) | ||
| <class 'sqlite3.Row'> | ||
| >>> tuple(r) | ||
| ('2006-01-05', 'BUY', 'RHAT', 100.0, 35.14) | ||
| >>> len(r) | ||
| 5 | ||
| >>> r[2] | ||
| 'RHAT' | ||
| >>> r.keys() | ||
| ['date', 'trans', 'symbol', 'qty', 'price'] | ||
| >>> r['qty'] | ||
| 100.0 | ||
| >>> for member in r: | ||
| ... print(member) | ||
| ... | ||
| 2006-01-05 | ||
| BUY | ||
| RHAT | ||
| 100.0 | ||
| 35.14 | ||
| >>> con = sqlite3.connect(":memory:") | ||
| >>> con.row_factory = sqlite3.Row | ||
| >>> res = con.execute("SELECT 'Earth' AS name, 6378 AS radius") | ||
| >>> row = res.fetchone() | ||
erlend-aasland marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| >>> row.keys() | ||
| ['name', 'radius'] | ||
| >>> row[0], row["name"] # Access by index and name. | ||
| ('Earth', 'Earth') | ||
| >>> row["RADIUS"] # Column names are case-insensitive. | ||
| 6378 | ||
| .. _sqlite3-blob-objects: | ||
| @@ -1766,20 +1742,6 @@ directly using only a single call on the :class:`Connection` object. | ||
| .. literalinclude:: ../includes/sqlite3/shortcut_methods.py | ||
| .. _sqlite3-columns-by-name: | ||
| Accessing columns by name instead of by index | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| One useful feature of the :mod:`!sqlite3` module is the built-in | ||
| :class:`sqlite3.Row` class designed to be used as a row factory. | ||
| Rows wrapped with this class can be accessed both by index (like tuples) and | ||
| case-insensitively by name: | ||
| .. literalinclude:: ../includes/sqlite3/rowclass.py | ||
| .. _sqlite3-connection-context-manager: | ||
| Using the connection as a context manager | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.