mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-27 11:24:33 +02:00
Fleshed out documentation for sqlite-utils convert a bit, refs #251
This commit is contained in:
parent
1b152951e5
commit
e5163a3b84
1 changed files with 15 additions and 1 deletions
16
docs/cli.rst
16
docs/cli.rst
|
|
@ -929,10 +929,24 @@ The ``convert`` command can be used to transform the data in a specified column
|
|||
|
||||
The command accepts a database, table, one or more columns and a string of Python code to be executed against the values from those columns. The following example would replace the values in the ``headline`` column in the ``articles`` table with an upper-case version::
|
||||
|
||||
sqlite-utils convert content.db articles headline 'value.upper()'
|
||||
$ sqlite-utils convert content.db articles headline 'value.upper()'
|
||||
|
||||
The Python code is passed as a string. Within that Python code the ``value`` variable will be the value of the current column.
|
||||
|
||||
The code you provide will be compiled into a function that takes ``value`` as a single argument. If you break your function body into multiple lines the last line should be a ``return`` statement::
|
||||
|
||||
$ sqlite-utils convert content.db articles headline '
|
||||
value = str(value)
|
||||
return value.upper()'
|
||||
|
||||
You can specify Python modules that should be imported and made available to your code using one or more ``--import`` options::
|
||||
|
||||
$ sqlite-utils convert content.db articles content \
|
||||
'"\n".join(textwrap.wrap(value, 10))' \
|
||||
--import=textwrap
|
||||
|
||||
The ``--dry-run`` option will output a preview of the conversion against the first ten rows, without modifying the database.
|
||||
|
||||
.. _cli_convert_output:
|
||||
|
||||
Saving the result to a different column
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue