Visitar URL original
gh-69093: Add context manager support to sqlite3.Blob by erlend-aasland · Pull Request #91562 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Simplify and relocate example
  • Loading branch information
Erlend E. Aasland
Erlend E. Aasland committed Apr 15, 2022
commit bce9c9fbda257021f464a2ce5e214cf40d66e223
15 changes: 8 additions & 7 deletions Doc/includes/sqlite3/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
con.execute("create table test(blob_col blob)")
con.execute("insert into test(blob_col) values (zeroblob(10))")

blob = con.blobopen("test", "blob_col", 1)
blob.write(b"Hello")
blob.write(b"World")
blob.seek(0)
print(blob.read()) # will print b"HelloWorld"
blob.close()
# Write to our blob, using two write operations:
with con.blobopen("test", "blob_col", 1) as blob:
blob.write(b"Hello")
blob.write(b"World")

# Read the contents of our blob
with con.blobopen("test", "blob_col", 1) as blob:
blob.write(b"UpdateBlob")
greeting = blob.read()

print(greeting) # outputs "b'HelloWorld'"
6 changes: 2 additions & 4 deletions Doc/library/sqlite3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,8 @@ Blob Objects
Use the :class:`Blob` as a :term:`context manager` to ensure that the blob
handle is closed after use.

.. literalinclude:: ../includes/sqlite3/blob.py

.. method:: close()

Close the blob.
Expand Down Expand Up @@ -1152,10 +1154,6 @@ Blob Objects
current position) and :data:`os.SEEK_END` (seek relative to the blob’s
end).

:class:`Blob` example:

.. literalinclude:: ../includes/sqlite3/blob.py


.. _sqlite3-types:

Expand Down