Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-90016: Reword sqlite3 adapter/converter docs#93095
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
598e26ae5b1b889399b5440bb59a82cf3e20b6f381c4dde4872013ef3c70d52681baadbf4cb1f2d1a0c194308ff06657f2b98e36397812bbd3dd5a2f381b6565eb45c172c7d95ac2af9433bf5af7646de6fbcff8b319b544e3b8fde821a7ebc295d89235f8d8484164b579f670e42fa68d97fcbd300b33File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -199,12 +199,12 @@ Module functions and constants | ||
| .. data:: PARSE_DECLTYPES | ||
| Use this flag with the *detect_types*parameter of :meth:`connect` to enable | ||
| parsing of declared types for each columnreturned. | ||
| The types are declared when the database table is created. | ||
erlend-aasland marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| ``sqlite3`` will look up a converter function using the first word of the | ||
| declared type as the converter dictionary key. | ||
| For example, the following SQL code results in the following lookups: | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| .. code-block:: sql | ||
| @@ -220,11 +220,11 @@ Module functions and constants | ||
| .. data:: PARSE_COLNAMES | ||
| Use this flag with the *detect_types*parameter of :meth:`connect` to enable | ||
| parsing of column names in queries. | ||
| ``sqlite3`` will look for strings containingsquarebrackets (``[]``), | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| and will look up aconverter function using the word inside the brackets as | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| the converterdictionary key. | ||
| .. code-block:: sql | ||
| @@ -262,8 +262,8 @@ Module functions and constants | ||
| Set it to any combination of :const:`PARSE_DECLTYPES` and | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| :const:`PARSE_COLNAMES` to enable type detection. | ||
| Types cannot be detected for generated fields (for example ``max(data)``), | ||
erlend-aasland marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| even whenthe*detect_types* parameter is set. | ||
| In such cases, the returned typeis :class:`str`. | ||
| By default, *check_same_thread* is :const:`True` and only the creating thread may | ||
| use the connection. If set :const:`False`, the returned connection may be shared | ||
| @@ -320,9 +320,9 @@ Module functions and constants | ||
| .. function:: register_converter(typename, converter) | ||
| Registerthe *converter*callableto convert SQLiteobjects of type *typename* into a | ||
| Pythonobject of a specifictype. The converter is invoked for all SQLite values of type | ||
| *typename*.Consult the parameter *detect_types* of | ||
| :meth:`Connection.connect` regarding how type detection works. | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| Note: *typename* and the name of the type in your query are matched in a | ||
| @@ -331,7 +331,7 @@ Module functions and constants | ||
| .. function:: register_adapter(type, adapter) | ||
| Registeran *adapter*callableto adapt the Python type *type* into an SQLite type. | ||
| The adapter is called with a Python object as its sole argument, | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| and must return a valid SQLite type: | ||
| :class:`int`, :class:`float`, :class:`str`, :class:`bytes`, or :const:`None`. | ||
| @@ -1219,12 +1219,13 @@ types via converters. | ||
| Using adapters to store custom Python types in SQLite databases | ||
erlend-aasland marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| SQLite supports only a limited set of types natively. | ||
| To store custom Python types in SQLite databases, **adapt** them one of the | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page.
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| basic types supported by SQLite: | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| :class:`int`, :class:`float`, :class:`str`, :class:`bytes`, or :const:`None`. | ||
| There are two ways to adapt Python objects to SQLite types: | ||
| letting your object adapt itself, or using an*adaptercallable*. | ||
| The latter will take precedence above the former. For a library that exports a | ||
| custom type, it may make sense to let that type be able to adapt itself. As an | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| application developer, it may make more sense to take control, and register | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| @@ -1236,20 +1237,21 @@ Letting your object adapt itself | ||
| Suppose we have ``Point`` class that represents a pair of coordinates, | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| ``x`` and ``y``, in a Cartesian coordinate system. | ||
| The coordinate pair will be stored as a text string in the database, | ||
| using a semicolon to separate the coordinates. | ||
| This can be implemented by adding a ``__conform__(self, protocol)`` | ||
| method which returns the adapted value. | ||
| The object passed to *protocol* will be of type :class:`PrepareProtocol`. | ||
erlend-aasland marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| .. literalinclude:: ../includes/sqlite3/adapter_point_1.py | ||
| Registering an adapter callable | ||
| """"""""""""""""""""""""""""""" | ||
| The other possibility is to create a function that converts the Python object | ||
| to anSQLite-compatible type. | ||
erlend-aasland marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| This function can then be registered using :meth:`register_adapter`. | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| .. literalinclude:: ../includes/sqlite3/adapter_point_2.py | ||
| @@ -1264,15 +1266,19 @@ but as a Unix timestamp. | ||
| Converting SQLite values to custom Python types | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| Writing an adapter lets you send custom Python types to SQLite. But to make it | ||
| really useful we need to make the Python to SQLite to Python roundtrip work. | ||
| To be able to convert SQLite value to custom Python types, we use _converters_. | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| Let's go back to the :class:`Point` class. We stored the x and y coordinates | ||
| separated via semicolons as strings in SQLite. | ||
| First, we'll define a converter function that accepts the string as a parameter | ||
| and constructs a :class:`Point` object from it. | ||
| .. note:: | ||
| Converter functionsare**always** passed a :class:`bytes` object, | ||
| no matter the underlying SQLite data type. | ||
| :: | ||
| @@ -1281,9 +1287,9 @@ We define a converter that accept a string, and return a ``Point`` object. | ||
| x, y = map(float, s.split(b";")) | ||
| return Point(x, y) | ||
| We now need to tell``sqlite3`` when it should convert a given SQLite value. | ||
| This is done when connecting to a database, using the *detect_types*parameter | ||
| of:meth:`connect`. We've got three options: | ||
| * Implicit: set *detect_types* to :const:`PARSE_DECLTYPES` | ||
| * Explicit: set *detect_types* to :const:`PARSE_COLNAMES` | ||
| @@ -1336,7 +1342,7 @@ This section shows recipes for common adapters and converters. | ||
| import sqlite3 | ||
| # Timezone-naive datetime adapters and converters. | ||
| def adapt_date(val): | ||
| return val.isoformat() | ||