/usr/share/doc/python-apsw/html/_sources/blob.txt is in python-apsw-doc 3.8.2-r1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | .. Automatically generated by code2rst.py
code2rst.py src/blob.c doc/blob.rst
Edit src/blob.c not this file!
.. currentmodule:: apsw
.. _blobio:
Blob Input/Output
*****************
A `blob <http://en.wikipedia.org/wiki/Binary_large_object>`_ is a
SQLite `datatype <https://sqlite.org/datatype3.html>`_ representing
a sequence of bytes. It can be zero or more bytes in size.
SQLite blobs have an absolute maximum size of 2GB and a `default
maximum size <https://sqlite.org/c3ref/c_limit_attached.html>`_ of
1GB.
An alternate approach to using blobs is to store the data in files and
store the filename in the database. Doing so loses the `ACID
<https://sqlite.org/transactional.html>`_ properties of SQLite.
zeroblob class
==============
.. class:: zeroblob(size)
If you want to insert a blob into a row, you previously needed to
supply the entire blob in one go. To read just one byte also
required retrieving the blob in its entireity. For example to insert
a 100MB file you would have done::
largedata=open("largefile", "rb").read()
cur.execute("insert into foo values(?)", (buffer(largedata),))
SQLite 3.5 allowed for incremental Blob I/O so you can read and
write blobs in small amounts. You cannot change the size of a blob
so you need to reserve space which you do through zeroblob which
creates a blob of the specified size but full of zero bytes. For
example you would reserve space for your 100MB one of these two
ways::
cur.execute("insert into foo values(zeroblob(100000000))")
cur.execute("insert into foo values(?),
(apsw.zeroblob(100000000),))
This class is used for the second way. Once a blob exists in the
database, you then use the :class:`blob` class to read and write its
contents.
.. method:: zeroblob.length() -> int
Size of zero blob in bytes.
blob class
==========
.. class:: blob
This object is created by :meth:`Connection.blobopen` and provides
access to a blob in the database. It behaves like a Python file.
At the C level it wraps a `sqlite3_blob
<https://sqlite.org/c3ref/blob.html>`_.
.. note::
You cannot change the size of a blob using this object. You should
create it with the correct size in advance either by using
:class:`zeroblob` or the `zeroblob()
<https://sqlite.org/lang_corefunc.html>`_ function.
See the :ref:`example <example-blobio>`.
.. method:: blob.__enter__() -> context
You can use a blob as a `context manager
<http://docs.python.org/reference/datamodel.html#with-statement-context-managers>`_
as defined in :pep:`0343`. When you use *with* statement,
the blob is always :meth:`closed <~blob.close>` on exit from the block, even if an
exception occurred in the block.
For example::
with connection.blobopen() as blob:
blob.write("...")
res=blob.read(1024)
.. method:: blob.__exit__() -> False
Implements context manager in conjunction with
:meth:`~blob.__enter__`. Any exception that happened in the
*with* block is raised after closing the blob.
.. index:: sqlite3_blob_close
.. method:: blob.close([force=False])
Closes the blob. Note that even if an error occurs the blob is
still closed.
.. note::
In some cases errors that technically occurred in the
:meth:`~blob.read` and :meth:`~blob.write` routines may not be
reported until close is called. Similarly errors that occurred
in those methods (eg calling :meth:`~blob.write` on a read-only
blob) may also be re-reported in :meth:`~blob.close`. (This
behaviour is what the underlying SQLite APIs do - it is not APSW
doing it.)
It is okay to call :meth:`~blob.close` multiple times.
:param force: Ignores any errors during close.
Calls: :sqliteapi:`sqlite3_blob_close <blob_close>`
.. index:: sqlite3_blob_bytes
.. method:: blob.length() -> int
Returns the size of the blob in bytes.
Calls: :sqliteapi:`sqlite3_blob_bytes <blob_bytes>`
.. index:: sqlite3_blob_read
.. method:: blob.read([nbytes]) -> bytes
Reads amount of data requested, or till end of file, whichever is
earlier. Attempting to read beyond the end of the blob returns the
empty string/bytes, in the same manner as end of file on normal file
objects.
:rtype: (Python 2) string (Python 3) bytes
Calls: :sqliteapi:`sqlite3_blob_read <blob_read>`
.. index:: sqlite3_blob_read
.. method:: blob.readinto(buffer[, offset=0, length=remaining-buffer]) -> None
Reads from the blob into a buffer you have supplied. This method is
useful if you already have a buffer like object that data is being
assembled in, and avoids allocating results in :meth:`blob.read` and
then copying into buffer.
:param buffer: A writable buffer like object. In Python 2.6 onwards
there is a bytearray type that is very useful.
:class:`array.array` also works.
:param offset: The position to start writing into the buffer
defaulting to the beginning.
:param length: How much of the blob to read. The default is the
remaining space left in the buffer. Note that if
there is more space available than blob left then you
will get a :exc:`ValueError` exception.
Calls: :sqliteapi:`sqlite3_blob_read <blob_read>`
.. index:: sqlite3_blob_reopen
.. method:: blob.reopen(rowid)
Change this blob object to point to a different row. It can be
faster than closing an existing blob an opening a new one.
Calls: :sqliteapi:`sqlite3_blob_reopen <blob_reopen>`
.. method:: blob.seek(offset[, whence=0]) -> None
Changes current position to *offset* biased by *whence*.
:param offset: New position to seek to. Can be positive or negative number.
:param whence: Use 0 if *offset* is relative to the begining of the blob,
1 if *offset* is relative to the current position,
and 2 if *offset* is relative to the end of the blob.
:raises ValueError: If the resulting offset is before the begining (less than zero) or beyond the end of the blob.
.. method:: blob.tell() -> int
Returns the current offset.
.. index:: sqlite3_blob_write
.. method:: blob.write(data) -> None
Writes the data to the blob.
:param data: (Python 2) buffer or string. (Python 3) buffer or bytes.
:raises TypeError: Wrong data type
:raises ValueError: If the data would go beyond the end of the blob.
You cannot increase the size of a blob by writing beyond the end.
You need to use :class:`zeroblob` to set the desired size first when
inserting the blob.
Calls: :sqliteapi:`sqlite3_blob_write <blob_write>`
|