@@ -199,12 +199,12 @@ Module functions and constants
199
199
200
200
.. data :: PARSE_DECLTYPES
201
201
202
- Use this flag with the *detect_types * keyword of :meth: `connect ` to enable
203
- parsing of declared types for each column it return .
202
+ Use this flag with the *detect_types * parameter of :meth: `connect ` to enable
203
+ parsing of declared types for each column returned .
204
204
The types are declared when the database table is created.
205
- :mod: ` sqlite3 ` will look up a converter function using the first word of the
205
+ `` sqlite3 ` ` will look up a converter function using the first word of the
206
206
declared type as the converter dictionary key.
207
- The following SQL code results in the following lookups:
207
+ For example, the following SQL code results in the following lookups:
208
208
209
209
.. code-block :: sql
210
210
@@ -220,11 +220,11 @@ Module functions and constants
220
220
221
221
.. data :: PARSE_COLNAMES
222
222
223
- Use this flag with the *detect_types * keyword of :meth: `connect ` to enable
223
+ Use this flag with the *detect_types * parameter of :meth: `connect ` to enable
224
224
parsing of column names in queries.
225
- :mod: ` sqlite3 ` will look for strings containing brackets, and will look up a
226
- converter function using the word inside the brackets as the converter
227
- dictionary key.
225
+ `` sqlite3 `` will look for strings containing square brackets (`` [] ``),
226
+ and will look up a converter function using the word inside the brackets as
227
+ the converter dictionary key.
228
228
229
229
.. code-block :: sql
230
230
@@ -262,8 +262,8 @@ Module functions and constants
262
262
Set it to any combination of :const: `PARSE_DECLTYPES ` and
263
263
:const: `PARSE_COLNAMES ` to enable type detection.
264
264
Types cannot be detected for generated fields (for example ``max(data) ``),
265
- even when *detect_types * parameter is set. In such cases, the returned type
266
- is :class: `str `.
265
+ even when the *detect_types * parameter is set.
266
+ In such cases, the returned type is :class: `str `.
267
267
268
268
By default, *check_same_thread * is :const: `True ` and only the creating thread may
269
269
use the connection. If set :const: `False `, the returned connection may be shared
@@ -320,9 +320,9 @@ Module functions and constants
320
320
321
321
.. function :: register_converter(typename, converter)
322
322
323
- Register callable *converter * to convert SQLite type name *typename * into a
324
- Python type. The converter is invoked for all SQLite values of type
325
- *typename *. Confer the parameter *detect_types * of
323
+ Register the *converter * callable to convert SQLite objects of type *typename * into a
324
+ Python object of a specific type. The converter is invoked for all SQLite values of type
325
+ *typename *. Consult the parameter *detect_types * of
326
326
:meth: `Connection.connect ` regarding how type detection works.
327
327
328
328
Note: *typename * and the name of the type in your query are matched in a
@@ -331,7 +331,7 @@ Module functions and constants
331
331
332
332
.. function :: register_adapter(type, adapter)
333
333
334
- Register callable *adapter * to adapt Python type *type * into an SQLite type.
334
+ Register an *adapter * callable to adapt the Python type *type * into an SQLite type.
335
335
The adapter is called with a Python object as its sole argument,
336
336
and must return a valid SQLite type:
337
337
:class: `int `, :class: `float `, :class: `str `, :class: `bytes `, or :const: `None `.
@@ -1219,12 +1219,13 @@ types via converters.
1219
1219
Using adapters to store custom Python types in SQLite databases
1220
1220
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1221
1221
1222
+ SQLite supports only a limited set of types natively.
1222
1223
To store custom Python types in SQLite databases, **adapt ** them one of the
1223
1224
basic types supported by SQLite:
1224
1225
:class: `int `, :class: `float `, :class: `str `, :class: `bytes `, or :const: `None `.
1225
1226
1226
1227
There are two ways to adapt Python objects to SQLite types:
1227
- letting your object adapt itself, or using an adapter function .
1228
+ letting your object adapt itself, or using an * adapter callable * .
1228
1229
The latter will take precedence above the former. For a library that exports a
1229
1230
custom type, it may make sense to let that type be able to adapt itself. As an
1230
1231
application developer, it may make more sense to take control, and register
@@ -1236,20 +1237,21 @@ Letting your object adapt itself
1236
1237
1237
1238
Suppose we have ``Point `` class that represents a pair of coordinates,
1238
1239
``x `` and ``y ``, in a Cartesian coordinate system.
1239
- We want to store the coordinate pair as a text string in the database,
1240
+ The coordinate pair will be stored as a text string in the database,
1240
1241
using a semicolon to separate the coordinates.
1241
- We implement this by adding a ``__conform__(self, protocol) `` method which
1242
- returns the adapted value. *protocol * will be :class: `PrepareProtocol `.
1242
+ This can be implemented by adding a ``__conform__(self, protocol) ``
1243
+ method which returns the adapted value.
1244
+ The object passed to *protocol * will be of type :class: `PrepareProtocol `.
1243
1245
1244
1246
.. literalinclude :: ../includes/sqlite3/adapter_point_1.py
1245
1247
1246
1248
1247
1249
Registering an adapter callable
1248
1250
"""""""""""""""""""""""""""""""
1249
1251
1250
- Continuing the above example, let's rewrite it using an adapter function.
1251
- We use :meth: ` register_adapter ` to add an adapter function that takes a Python
1252
- type as its argument, and returns an SQLite compatible type .
1252
+ The other possibility is to create a function that converts the Python object
1253
+ to an SQLite-compatible type.
1254
+ This function can then be registered using :meth: ` register_adapter ` .
1253
1255
1254
1256
.. literalinclude :: ../includes/sqlite3/adapter_point_2.py
1255
1257
@@ -1264,15 +1266,19 @@ but as a Unix timestamp.
1264
1266
Converting SQLite values to custom Python types
1265
1267
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1266
1268
1269
+ Writing an adapter lets you send custom Python types to SQLite. But to make it
1270
+ really useful we need to make the Python to SQLite to Python roundtrip work.
1267
1271
To be able to convert SQLite value to custom Python types, we use _converters_.
1268
1272
1269
- Let's revisit the ``Point `` class example from above;
1270
- the coordinate pair is stored in the database as a semicolon separated string.
1271
- We define a converter that accept a string, and return a ``Point `` object.
1273
+ Let's go back to the :class: `Point ` class. We stored the x and y coordinates
1274
+ separated via semicolons as strings in SQLite.
1275
+
1276
+ First, we'll define a converter function that accepts the string as a parameter
1277
+ and constructs a :class: `Point ` object from it.
1272
1278
1273
1279
.. note ::
1274
1280
1275
- Converter functions **always ** are passed a :class: `bytes ` object,
1281
+ Converter functions are **always ** passed a :class: `bytes ` object,
1276
1282
no matter the underlying SQLite data type.
1277
1283
1278
1284
::
@@ -1281,9 +1287,9 @@ We define a converter that accept a string, and return a ``Point`` object.
1281
1287
x, y = map(float, s.split(b";"))
1282
1288
return Point(x, y)
1283
1289
1284
- We now need to tell :mod: ` sqlite3 ` when it should convert a given SQLite value.
1285
- This is done when connecting to a database, using the *detect_types * keyword of
1286
- :meth: `connect `. We've got three options:
1290
+ We now need to tell `` sqlite3 ` ` when it should convert a given SQLite value.
1291
+ This is done when connecting to a database, using the *detect_types * parameter
1292
+ of :meth: `connect `. We've got three options:
1287
1293
1288
1294
* Implicit: set *detect_types * to :const: `PARSE_DECLTYPES `
1289
1295
* Explicit: set *detect_types * to :const: `PARSE_COLNAMES `
@@ -1336,7 +1342,7 @@ This section shows recipes for common adapters and converters.
1336
1342
1337
1343
import sqlite3
1338
1344
1339
- # Timezone naive datetime adapters and converters.
1345
+ # Timezone- naive datetime adapters and converters.
1340
1346
def adapt_date(val):
1341
1347
return val.isoformat()
1342
1348
0 commit comments