Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
272 changes: 136 additions & 136 deletions docs/cli.rst

Large diffs are not rendered by default.

138 changes: 69 additions & 69 deletions docs/python-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,8 @@ The ``db.schema`` property returns the full SQL schema for the database as a str
>>> db = sqlite_utils.Database("dogs.db")
>>> print(db.schema)
CREATE TABLE "dogs" (
[id] INTEGER PRIMARY KEY,
[name] TEXT
"id" INTEGER PRIMARY KEY,
"name" TEXT
);

.. _python_api_creating_tables:
Expand Down Expand Up @@ -544,11 +544,11 @@ This will create a table with the following schema:

.. code-block:: sql

CREATE TABLE [dogs] (
[id] INTEGER PRIMARY KEY,
[name] TEXT,
[age] INTEGER,
[weight] FLOAT
CREATE TABLE "dogs" (
"id" INTEGER PRIMARY KEY,
"name" TEXT,
"age" INTEGER,
"weight" FLOAT
)

.. _python_api_explicit_create:
Expand Down Expand Up @@ -729,10 +729,10 @@ Here's an example that uses these features:
# {'id': 3, 'name': 'Dharma', 'score': 1}]
print(db.table("authors").schema)
# Outputs:
# CREATE TABLE [authors] (
# [id] INTEGER PRIMARY KEY,
# [name] TEXT NOT NULL,
# [score] INTEGER NOT NULL DEFAULT 1
# CREATE TABLE "authors" (
# "id" INTEGER PRIMARY KEY,
# "name" TEXT NOT NULL,
# "score" INTEGER NOT NULL DEFAULT 1
# )


Expand Down Expand Up @@ -1196,10 +1196,10 @@ You can inspect the database to see the results like this::
>>> list(db.table("characteristics_dogs").rows)
[{'characteristics_id': 1, 'dogs_id': 1}, {'characteristics_id': 2, 'dogs_id': 1}]
>>> print(db.table("characteristics_dogs").schema)
CREATE TABLE [characteristics_dogs] (
[characteristics_id] INTEGER REFERENCES [characteristics]([id]),
[dogs_id] INTEGER REFERENCES [dogs]([id]),
PRIMARY KEY ([characteristics_id], [dogs_id])
CREATE TABLE "characteristics_dogs" (
"characteristics_id" INTEGER REFERENCES "characteristics"("id"),
"dogs_id" INTEGER REFERENCES "dogs"("id"),
PRIMARY KEY ("characteristics_id", "dogs_id")
)

.. _python_api_analyze_column:
Expand Down Expand Up @@ -1648,10 +1648,10 @@ The schema of the above table is:

.. code-block:: sql

CREATE TABLE [Trees] (
[id] INTEGER PRIMARY KEY,
[TreeAddress] TEXT,
[Species] TEXT
CREATE TABLE "Trees" (
"id" INTEGER PRIMARY KEY,
"TreeAddress" TEXT,
"Species" TEXT
)

Here's how to extract the ``Species`` column using ``.extract()``:
Expand All @@ -1665,19 +1665,19 @@ After running this code the table schema now looks like this:
.. code-block:: sql

CREATE TABLE "Trees" (
[id] INTEGER PRIMARY KEY,
[TreeAddress] TEXT,
[Species_id] INTEGER,
"id" INTEGER PRIMARY KEY,
"TreeAddress" TEXT,
"Species_id" INTEGER,
FOREIGN KEY(Species_id) REFERENCES Species(id)
)

A new ``Species`` table will have been created with the following schema:

.. code-block:: sql

CREATE TABLE [Species] (
[id] INTEGER PRIMARY KEY,
[Species] TEXT
CREATE TABLE "Species" (
"id" INTEGER PRIMARY KEY,
"Species" TEXT
)

The ``.extract()`` method defaults to creating a table with the same name as the column that was extracted, and adding a foreign key column called ``tablename_id``.
Expand All @@ -1693,15 +1693,15 @@ The resulting schema looks like this:
.. code-block:: sql

CREATE TABLE "Trees" (
[id] INTEGER PRIMARY KEY,
[TreeAddress] TEXT,
[tree_species_id] INTEGER,
"id" INTEGER PRIMARY KEY,
"TreeAddress" TEXT,
"tree_species_id" INTEGER,
FOREIGN KEY(tree_species_id) REFERENCES tree_species(id)
)

CREATE TABLE [tree_species] (
[id] INTEGER PRIMARY KEY,
[Species] TEXT
CREATE TABLE "tree_species" (
"id" INTEGER PRIMARY KEY,
"Species" TEXT
)

You can also extract multiple columns into the same external table. Say for example you have a table like this:
Expand All @@ -1726,15 +1726,15 @@ This produces the following schema:
.. code-block:: sql

CREATE TABLE "Trees" (
[id] INTEGER PRIMARY KEY,
[TreeAddress] TEXT,
[CommonName_LatinName_id] INTEGER,
"id" INTEGER PRIMARY KEY,
"TreeAddress" TEXT,
"CommonName_LatinName_id" INTEGER,
FOREIGN KEY(CommonName_LatinName_id) REFERENCES CommonName_LatinName(id)
)
CREATE TABLE [CommonName_LatinName] (
[id] INTEGER PRIMARY KEY,
[CommonName] TEXT,
[LatinName] TEXT
CREATE TABLE "CommonName_LatinName" (
"id" INTEGER PRIMARY KEY,
"CommonName" TEXT,
"LatinName" TEXT
)

The table name ``CommonName_LatinName`` is derived from the extract columns. You can use ``table=`` and ``fk_column=`` to specify custom names like this:
Expand All @@ -1748,15 +1748,15 @@ This produces the following schema:
.. code-block:: sql

CREATE TABLE "Trees" (
[id] INTEGER PRIMARY KEY,
[TreeAddress] TEXT,
[species_id] INTEGER,
"id" INTEGER PRIMARY KEY,
"TreeAddress" TEXT,
"species_id" INTEGER,
FOREIGN KEY(species_id) REFERENCES Species(id)
)
CREATE TABLE [Species] (
[id] INTEGER PRIMARY KEY,
[CommonName] TEXT,
[LatinName] TEXT
CREATE TABLE "Species" (
"id" INTEGER PRIMARY KEY,
"CommonName" TEXT,
"LatinName" TEXT
)

You can use the ``rename=`` argument to rename columns in the lookup table. To create a ``Species`` table with columns called ``name`` and ``latin`` you can do this:
Expand All @@ -1774,10 +1774,10 @@ This produces a lookup table like so:

.. code-block:: sql

CREATE TABLE [Species] (
[id] INTEGER PRIMARY KEY,
[name] TEXT,
[latin] TEXT
CREATE TABLE "Species" (
"id" INTEGER PRIMARY KEY,
"name" TEXT,
"latin" TEXT
)

.. _python_api_hash:
Expand Down Expand Up @@ -2100,12 +2100,12 @@ The ``.schema`` property outputs the table's schema as a SQL string::
"Longitude" REAL,
"Location" TEXT
,
FOREIGN KEY ("PlantType") REFERENCES [PlantType](id),
FOREIGN KEY ("qCaretaker") REFERENCES [qCaretaker](id),
FOREIGN KEY ("qSpecies") REFERENCES [qSpecies](id),
FOREIGN KEY ("qSiteInfo") REFERENCES [qSiteInfo](id),
FOREIGN KEY ("qCareAssistant") REFERENCES [qCareAssistant](id),
FOREIGN KEY ("qLegalStatus") REFERENCES [qLegalStatus](id))
FOREIGN KEY ("PlantType") REFERENCES "PlantType"(id),
FOREIGN KEY ("qCaretaker") REFERENCES "qCaretaker"(id),
FOREIGN KEY ("qSpecies") REFERENCES "qSpecies"(id),
FOREIGN KEY ("qSiteInfo") REFERENCES "qSiteInfo"(id),
FOREIGN KEY ("qCareAssistant") REFERENCES "qCareAssistant"(id),
FOREIGN KEY ("qLegalStatus") REFERENCES "qLegalStatus"(id))

.. _python_api_introspection_strict:

Expand Down Expand Up @@ -2507,9 +2507,9 @@ This will create the ``_counts`` table if it does not already exist, with the fo

.. code-block:: sql

CREATE TABLE [_counts] (
[table] TEXT PRIMARY KEY,
[count] INTEGER DEFAULT 0
CREATE TABLE "_counts" (
"table" TEXT PRIMARY KEY,
"count" INTEGER DEFAULT 0
)

You can enable cached counts for every table in a database (except for virtual tables and the ``_counts`` table itself) using the database ``enable_counts()`` method:
Expand Down Expand Up @@ -2719,11 +2719,11 @@ For example:

# The table schema looks like this:
# print(db.table("cats").schema)
# CREATE TABLE [cats] (
# [id] INTEGER PRIMARY KEY,
# [name] TEXT,
# [age] INTEGER,
# [thumbnail] BLOB
# CREATE TABLE "cats" (
# "id" INTEGER PRIMARY KEY,
# "name" TEXT,
# "age" INTEGER,
# "thumbnail" BLOB
# )

.. _python_api_register_function:
Expand Down Expand Up @@ -2887,9 +2887,9 @@ If we insert this data directly into a table we will get a schema that is entire
db.table("creatures").insert_all(rows)
print(db.schema)
# Outputs:
# CREATE TABLE [creatures] (
# [id] TEXT,
# [name] TEXT
# CREATE TABLE "creatures" (
# "id" TEXT,
# "name" TEXT
# );

We can detect the best column types using a ``TypeTracker`` instance:
Expand All @@ -2910,9 +2910,9 @@ We can then apply those types to our new table using the :ref:`table.transform()
db.table("creatures2").transform(types=tracker.types)
print(db.table("creatures2").schema)
# Outputs:
# CREATE TABLE [creatures2] (
# [id] INTEGER,
# [name] TEXT
# CREATE TABLE "creatures2" (
# "id" INTEGER,
# "name" TEXT
# );

.. _python_api_gis:
Expand Down
36 changes: 18 additions & 18 deletions docs/tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CREATE TABLE [creatures] (\n",
" [name] TEXT,\n",
" [species] TEXT,\n",
" [age] FLOAT\n",
"CREATE TABLE \"creatures\" (\n",
" \"name\" TEXT,\n",
" \"species\" TEXT,\n",
" \"age\" FLOAT\n",
")\n"
]
}
Expand Down Expand Up @@ -534,11 +534,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CREATE TABLE [creatures] (\n",
" [id] INTEGER PRIMARY KEY,\n",
" [name] TEXT,\n",
" [species] TEXT,\n",
" [age] FLOAT\n",
"CREATE TABLE \"creatures\" (\n",
" \"id\" INTEGER PRIMARY KEY,\n",
" \"name\" TEXT,\n",
" \"species\" TEXT,\n",
" \"age\" FLOAT\n",
")\n"
]
}
Expand Down Expand Up @@ -929,11 +929,11 @@
"output_type": "stream",
"text": [
"CREATE TABLE \"creatures\" (\n",
" [id] INTEGER PRIMARY KEY,\n",
" [name] TEXT,\n",
" [species_id] INTEGER,\n",
" [age] FLOAT,\n",
" FOREIGN KEY([species_id]) REFERENCES [species]([id])\n",
" \"id\" INTEGER PRIMARY KEY,\n",
" \"name\" TEXT,\n",
" \"species_id\" INTEGER,\n",
" \"age\" FLOAT,\n",
" FOREIGN KEY(\"species_id\") REFERENCES \"species\"(\"id\")\n",
")\n",
"[{'id': 1, 'name': 'Cleo', 'species_id': 1, 'age': 6.0}, {'id': 2, 'name': 'Lila', 'species_id': 2, 'age': 0.8}, {'id': 3, 'name': 'Bants', 'species_id': 2, 'age': 0.8}, {'id': 4, 'name': 'Azi', 'species_id': 2, 'age': 0.8}, {'id': 5, 'name': 'Snowy', 'species_id': 2, 'age': 0.9}, {'id': 6, 'name': 'Blue', 'species_id': 2, 'age': 0.9}]\n"
]
Expand Down Expand Up @@ -962,9 +962,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CREATE TABLE [species] (\n",
" [id] INTEGER PRIMARY KEY,\n",
" [species] TEXT\n",
"CREATE TABLE \"species\" (\n",
" \"id\" INTEGER PRIMARY KEY,\n",
" \"species\" TEXT\n",
")\n",
"[{'id': 1, 'species': 'dog'}, {'id': 2, 'species': 'chicken'}]\n"
]
Expand Down Expand Up @@ -1048,4 +1048,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
20 changes: 14 additions & 6 deletions sqlite_utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
import pathlib
from runpy import run_module
import sqlite_utils
from sqlite_utils.db import AlterError, BadMultiValues, DescIndex, NoTable
from sqlite_utils.db import (
AlterError,
BadMultiValues,
DescIndex,
NoTable,
quote_identifier,
)
from sqlite_utils.plugins import pm, get_plugins
from sqlite_utils.utils import maximize_csv_field_size_limit
from sqlite_utils import recipes
Expand Down Expand Up @@ -1967,7 +1973,9 @@ def memory(
view_names.append("t")
for view_name in view_names:
if not db[view_name].exists():
db.create_view(view_name, "select * from [{}]".format(file_table))
db.create_view(
view_name, "select * from {}".format(quote_identifier(file_table))
)

if fp:
fp.close()
Expand Down Expand Up @@ -2230,8 +2238,8 @@ def rows(
"""
columns = "*"
if column:
columns = ", ".join("[{}]".format(c) for c in column)
sql = "select {} from [{}]".format(columns, dbtable)
columns = ", ".join(quote_identifier(c) for c in column)
sql = "select {} from {}".format(columns, quote_identifier(dbtable))
if where:
sql += " where " + where
if order:
Expand Down Expand Up @@ -2288,10 +2296,10 @@ def triggers(
\b
sqlite-utils triggers trees.db
"""
sql = "select name, tbl_name as [table], sql from sqlite_master where type = 'trigger'"
sql = "select name, tbl_name as \"table\", sql from sqlite_master where type = 'trigger'"
if tables:
quote = sqlite_utils.Database(memory=True).quote
sql += " and [table] in ({})".format(
sql += ' and "table" in ({})'.format(
", ".join(quote(table) for table in tables)
)
ctx.invoke(
Expand Down
Loading
Loading