@@ -1233,6 +1233,38 @@ and you can let the :mod:`!sqlite3` module convert SQLite types to
1233
1233
Python types via :ref: `converters <sqlite3-converters >`.
1234
1234
1235
1235
1236
+ .. _sqlite3-default-converters :
1237
+
1238
+ Default adapters and converters
1239
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1240
+
1241
+ There are default adapters for the date and datetime types in the datetime
1242
+ module. They will be sent as ISO dates/ISO timestamps to SQLite.
1243
+
1244
+ The default converters are registered under the name "date" for
1245
+ :class: `datetime.date ` and under the name "timestamp" for
1246
+ :class: `datetime.datetime `.
1247
+
1248
+ This way, you can use date/timestamps from Python without any additional
1249
+ fiddling in most cases. The format of the adapters is also compatible with the
1250
+ experimental SQLite date/time functions.
1251
+
1252
+ The following example demonstrates this.
1253
+
1254
+ .. literalinclude :: ../includes/sqlite3/pysqlite_datetime.py
1255
+
1256
+ If a timestamp stored in SQLite has a fractional part longer than 6
1257
+ numbers, its value will be truncated to microsecond precision by the
1258
+ timestamp converter.
1259
+
1260
+ .. note ::
1261
+
1262
+ The default "timestamp" converter ignores UTC offsets in the database and
1263
+ always returns a naive :class: `datetime.datetime ` object. To preserve UTC
1264
+ offsets in timestamps, either leave converters disabled, or register an
1265
+ offset-aware converter with :func: `register_converter `.
1266
+
1267
+
1236
1268
.. _sqlite3-howtos :
1237
1269
1238
1270
How-to guides
@@ -1270,8 +1302,8 @@ both styles:
1270
1302
1271
1303
.. _sqlite3-adapters :
1272
1304
1273
- Using adapters to store custom Python types in SQLite databases
1274
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1305
+ How to adapt custom Python types to SQLite values
1306
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1275
1307
1276
1308
SQLite supports only a limited set of data types natively.
1277
1309
To store custom Python types in SQLite databases, *adapt * them to one of the
@@ -1288,8 +1320,8 @@ registering custom adapter functions.
1288
1320
1289
1321
.. _sqlite3-conform :
1290
1322
1291
- Letting your object adapt itself
1292
- """"""""""""""""""""""""""""""""
1323
+ How to write adaptable objects
1324
+ """"""""""""""""""""""""""""""
1293
1325
1294
1326
Suppose we have a :class: `!Point ` class that represents a pair of coordinates,
1295
1327
``x `` and ``y ``, in a Cartesian coordinate system.
@@ -1302,8 +1334,8 @@ The object passed to *protocol* will be of type :class:`PrepareProtocol`.
1302
1334
.. literalinclude :: ../includes/sqlite3/adapter_point_1.py
1303
1335
1304
1336
1305
- Registering an adapter callable
1306
- """""""""""""""""""""""""""""""
1337
+ How to register adapter callables
1338
+ """""""""""""""""""""""""""""""""
1307
1339
1308
1340
The other possibility is to create a function that converts the Python object
1309
1341
to an SQLite-compatible type.
@@ -1314,8 +1346,8 @@ This function can then be registered using :func:`register_adapter`.
1314
1346
1315
1347
.. _sqlite3-converters :
1316
1348
1317
- Converting SQLite values to custom Python types
1318
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1349
+ How to convert SQLite values to custom Python types
1350
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1319
1351
1320
1352
Writing an adapter lets you convert *from * custom Python types *to * SQLite
1321
1353
values.
@@ -1354,36 +1386,6 @@ The following example illustrates the implicit and explicit approaches:
1354
1386
.. literalinclude :: ../includes/sqlite3/converter_point.py
1355
1387
1356
1388
1357
- Default adapters and converters
1358
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1359
-
1360
- There are default adapters for the date and datetime types in the datetime
1361
- module. They will be sent as ISO dates/ISO timestamps to SQLite.
1362
-
1363
- The default converters are registered under the name "date" for
1364
- :class: `datetime.date ` and under the name "timestamp" for
1365
- :class: `datetime.datetime `.
1366
-
1367
- This way, you can use date/timestamps from Python without any additional
1368
- fiddling in most cases. The format of the adapters is also compatible with the
1369
- experimental SQLite date/time functions.
1370
-
1371
- The following example demonstrates this.
1372
-
1373
- .. literalinclude :: ../includes/sqlite3/pysqlite_datetime.py
1374
-
1375
- If a timestamp stored in SQLite has a fractional part longer than 6
1376
- numbers, its value will be truncated to microsecond precision by the
1377
- timestamp converter.
1378
-
1379
- .. note ::
1380
-
1381
- The default "timestamp" converter ignores UTC offsets in the database and
1382
- always returns a naive :class: `datetime.datetime ` object. To preserve UTC
1383
- offsets in timestamps, either leave converters disabled, or register an
1384
- offset-aware converter with :func: `register_converter `.
1385
-
1386
-
1387
1389
.. _sqlite3-adapter-converter-recipes :
1388
1390
1389
1391
Adapter and converter recipes
@@ -1431,8 +1433,8 @@ This section shows recipes for common adapters and converters.
1431
1433
1432
1434
.. _sqlite3-connection-shortcuts :
1433
1435
1434
- Using connection shortcut methods
1435
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1436
+ How to use connection shortcut methods
1437
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1436
1438
1437
1439
Using the :meth: `~Connection.execute `,
1438
1440
:meth: `~Connection.executemany `, and :meth: `~Connection.executescript `
@@ -1448,7 +1450,7 @@ directly using only a single call on the :class:`Connection` object.
1448
1450
1449
1451
.. _sqlite3-connection-context-manager :
1450
1452
1451
- Using the connection as a context manager
1453
+ How to use the connection context manager
1452
1454
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1453
1455
1454
1456
A :class: `Connection ` object can be used as a context manager that
@@ -1473,8 +1475,8 @@ the context manager is a no-op.
1473
1475
1474
1476
.. _sqlite3-uri-tricks :
1475
1477
1476
- Working with SQLite URIs
1477
- ^^^^^^^^^^^^^^^^^^^^^^^^
1478
+ How to work with SQLite URIs
1479
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1478
1480
1479
1481
Some useful URI tricks include:
1480
1482
0 commit comments