mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-24 01:44:31 +02:00
parent
bf1ac778a3
commit
fb93452ea8
20 changed files with 910 additions and 849 deletions
272
docs/cli.rst
272
docs/cli.rst
|
|
@ -547,13 +547,13 @@ To see the in-memory database schema that would be used for a file or for multip
|
|||
|
||||
.. code-block:: output
|
||||
|
||||
CREATE TABLE [dogs] (
|
||||
[id] INTEGER,
|
||||
[age] INTEGER,
|
||||
[name] TEXT
|
||||
CREATE TABLE "dogs" (
|
||||
"id" INTEGER,
|
||||
"age" INTEGER,
|
||||
"name" TEXT
|
||||
);
|
||||
CREATE VIEW t1 AS select * from [dogs];
|
||||
CREATE VIEW t AS select * from [dogs];
|
||||
CREATE VIEW "t1" AS select * from "dogs";
|
||||
CREATE VIEW "t" AS select * from "dogs";
|
||||
|
||||
You can run the equivalent of the :ref:`analyze-tables <cli_analyze_tables>` command using ``--analyze``:
|
||||
|
||||
|
|
@ -596,15 +596,15 @@ You can output SQL that will both create the tables and insert the full data use
|
|||
.. code-block:: output
|
||||
|
||||
BEGIN TRANSACTION;
|
||||
CREATE TABLE [dogs] (
|
||||
[id] INTEGER,
|
||||
[age] INTEGER,
|
||||
[name] TEXT
|
||||
CREATE TABLE "dogs" (
|
||||
"id" INTEGER,
|
||||
"age" INTEGER,
|
||||
"name" TEXT
|
||||
);
|
||||
INSERT INTO "dogs" VALUES('1','4','Cleo');
|
||||
INSERT INTO "dogs" VALUES('2','2','Pancakes');
|
||||
CREATE VIEW t1 AS select * from [dogs];
|
||||
CREATE VIEW t AS select * from [dogs];
|
||||
CREATE VIEW "t1" AS select * from "dogs";
|
||||
CREATE VIEW "t" AS select * from "dogs";
|
||||
COMMIT;
|
||||
|
||||
Passing ``--save other.db`` will instead use that SQL to populate a new database file:
|
||||
|
|
@ -746,10 +746,10 @@ Use ``--schema`` to include the schema of each table:
|
|||
------- -----------------------------------------------
|
||||
Gosh CREATE TABLE Gosh (c1 text, c2 text, c3 text)
|
||||
Gosh2 CREATE TABLE Gosh2 (c1 text, c2 text, c3 text)
|
||||
dogs CREATE TABLE [dogs] (
|
||||
[id] INTEGER,
|
||||
[age] INTEGER,
|
||||
[name] TEXT)
|
||||
dogs CREATE TABLE "dogs" (
|
||||
"id" INTEGER,
|
||||
"age" INTEGER,
|
||||
"name" TEXT)
|
||||
|
||||
The ``--nl``, ``--csv``, ``--tsv``, ``--table`` and ``--fmt`` options are also available.
|
||||
|
||||
|
|
@ -839,13 +839,13 @@ The ``triggers`` command shows any triggers configured for the database:
|
|||
|
||||
name table sql
|
||||
--------------- --------- -----------------------------------------------------------------
|
||||
plants_insert plants CREATE TRIGGER [plants_insert] AFTER INSERT ON [plants]
|
||||
plants_insert plants CREATE TRIGGER "plants_insert" AFTER INSERT ON "plants"
|
||||
BEGIN
|
||||
INSERT OR REPLACE INTO [_counts]
|
||||
INSERT OR REPLACE INTO "_counts"
|
||||
VALUES (
|
||||
'plants',
|
||||
COALESCE(
|
||||
(SELECT count FROM [_counts] WHERE [table] = 'plants'),
|
||||
(SELECT count FROM "_counts" WHERE "table" = 'plants'),
|
||||
0
|
||||
) + 1
|
||||
);
|
||||
|
|
@ -876,8 +876,8 @@ The ``sqlite-utils schema`` command shows the full SQL schema for the database:
|
|||
.. code-block:: output
|
||||
|
||||
CREATE TABLE "dogs" (
|
||||
[id] INTEGER PRIMARY KEY,
|
||||
[name] TEXT
|
||||
"id" INTEGER PRIMARY KEY,
|
||||
"name" TEXT
|
||||
);
|
||||
|
||||
This will show the schema for every table and index in the database. To view the schema just for a specified subset of tables pass those as additional arguments:
|
||||
|
|
@ -983,16 +983,16 @@ The ``_analyze_tables_`` table has the following schema:
|
|||
|
||||
.. code-block:: sql
|
||||
|
||||
CREATE TABLE [_analyze_tables_] (
|
||||
[table] TEXT,
|
||||
[column] TEXT,
|
||||
[total_rows] INTEGER,
|
||||
[num_null] INTEGER,
|
||||
[num_blank] INTEGER,
|
||||
[num_distinct] INTEGER,
|
||||
[most_common] TEXT,
|
||||
[least_common] TEXT,
|
||||
PRIMARY KEY ([table], [column])
|
||||
CREATE TABLE "_analyze_tables_" (
|
||||
"table" TEXT,
|
||||
"column" TEXT,
|
||||
"total_rows" INTEGER,
|
||||
"num_null" INTEGER,
|
||||
"num_blank" INTEGER,
|
||||
"num_distinct" INTEGER,
|
||||
"most_common" TEXT,
|
||||
"least_common" TEXT,
|
||||
PRIMARY KEY ("table", "column")
|
||||
);
|
||||
|
||||
The ``most_common`` and ``least_common`` columns will contain nested JSON arrays of the most common and least common values that look like this:
|
||||
|
|
@ -1197,23 +1197,23 @@ Inserting this into a table using ``sqlite-utils insert logs.db logs log.json``
|
|||
|
||||
.. code-block:: sql
|
||||
|
||||
CREATE TABLE [logs] (
|
||||
[httpRequest] TEXT,
|
||||
[insertId] TEXT,
|
||||
[labels] TEXT
|
||||
CREATE TABLE "logs" (
|
||||
"httpRequest" TEXT,
|
||||
"insertId" TEXT,
|
||||
"labels" TEXT
|
||||
);
|
||||
|
||||
With the ``--flatten`` option columns will be created using ``topkey_nextkey`` column names - so running ``sqlite-utils insert logs.db logs log.json --flatten`` will create the following schema instead:
|
||||
|
||||
.. code-block:: sql
|
||||
|
||||
CREATE TABLE [logs] (
|
||||
[httpRequest_latency] TEXT,
|
||||
[httpRequest_requestMethod] TEXT,
|
||||
[httpRequest_requestSize] TEXT,
|
||||
[httpRequest_status] INTEGER,
|
||||
[insertId] TEXT,
|
||||
[labels_service] TEXT
|
||||
CREATE TABLE "logs" (
|
||||
"httpRequest_latency" TEXT,
|
||||
"httpRequest_requestMethod" TEXT,
|
||||
"httpRequest_requestSize" TEXT,
|
||||
"httpRequest_status" INTEGER,
|
||||
"insertId" TEXT,
|
||||
"labels_service" TEXT
|
||||
);
|
||||
|
||||
.. _cli_insert_csv_tsv:
|
||||
|
|
@ -1272,9 +1272,9 @@ Will produce this schema:
|
|||
.. code-block:: output
|
||||
|
||||
CREATE TABLE "creatures" (
|
||||
[name] TEXT,
|
||||
[age] INTEGER,
|
||||
[weight] FLOAT
|
||||
"name" TEXT,
|
||||
"age" INTEGER,
|
||||
"weight" FLOAT
|
||||
);
|
||||
|
||||
You can set the ``SQLITE_UTILS_DETECT_TYPES`` environment variable if you want ``--detect-types`` to be the default behavior:
|
||||
|
|
@ -1354,8 +1354,8 @@ This will produce the following schema:
|
|||
|
||||
.. code-block:: sql
|
||||
|
||||
CREATE TABLE [loglines] (
|
||||
[line] TEXT
|
||||
CREATE TABLE "loglines" (
|
||||
"line" TEXT
|
||||
);
|
||||
|
||||
You can also insert the entire contents of the file into a single column called ``text`` using ``--text``:
|
||||
|
|
@ -1368,8 +1368,8 @@ The schema here will be:
|
|||
|
||||
.. code-block:: sql
|
||||
|
||||
CREATE TABLE [content] (
|
||||
[text] TEXT
|
||||
CREATE TABLE "content" (
|
||||
"text" TEXT
|
||||
);
|
||||
|
||||
.. _cli_insert_convert:
|
||||
|
|
@ -1462,8 +1462,8 @@ The result looks like this:
|
|||
.. code-block:: output
|
||||
|
||||
BEGIN TRANSACTION;
|
||||
CREATE TABLE [words] (
|
||||
[word] TEXT
|
||||
CREATE TABLE "words" (
|
||||
"word" TEXT
|
||||
);
|
||||
INSERT INTO "words" VALUES('A');
|
||||
INSERT INTO "words" VALUES('bunch');
|
||||
|
|
@ -1568,10 +1568,10 @@ By default this command will create a table with the following schema:
|
|||
|
||||
.. code-block:: sql
|
||||
|
||||
CREATE TABLE [images] (
|
||||
[path] TEXT PRIMARY KEY,
|
||||
[content] BLOB,
|
||||
[size] INTEGER
|
||||
CREATE TABLE "images" (
|
||||
"path" TEXT PRIMARY KEY,
|
||||
"content" BLOB,
|
||||
"size" INTEGER
|
||||
);
|
||||
|
||||
Content will be treated as binary by default and stored in a ``BLOB`` column. You can use the ``--text`` option to store that content in a ``TEXT`` column instead.
|
||||
|
|
@ -1586,10 +1586,10 @@ This will result in the following schema:
|
|||
|
||||
.. code-block:: sql
|
||||
|
||||
CREATE TABLE [images] (
|
||||
[path] TEXT PRIMARY KEY,
|
||||
[md5] TEXT,
|
||||
[mtime] FLOAT
|
||||
CREATE TABLE "images" (
|
||||
"path" TEXT PRIMARY KEY,
|
||||
"md5" TEXT,
|
||||
"mtime" FLOAT
|
||||
);
|
||||
|
||||
Note that there's no ``content`` column here at all - if you specify custom columns using ``-c`` you need to include ``-c content`` to create that column.
|
||||
|
|
@ -1886,10 +1886,10 @@ The type of the returned values will be taken into account when creating the new
|
|||
|
||||
.. code-block:: sql
|
||||
|
||||
CREATE TABLE [places] (
|
||||
[location] TEXT,
|
||||
[latitude] FLOAT,
|
||||
[longitude] FLOAT
|
||||
CREATE TABLE "places" (
|
||||
"location" TEXT,
|
||||
"latitude" FLOAT,
|
||||
"longitude" FLOAT
|
||||
);
|
||||
|
||||
The code function can also return ``None``, in which case its output will be ignored. You can drop the original column at the end of the operation by adding ``--drop``.
|
||||
|
|
@ -1933,11 +1933,11 @@ You can specify columns that should be NOT NULL using ``--not-null colname``. Yo
|
|||
|
||||
table schema
|
||||
------- --------------------------------
|
||||
mytable CREATE TABLE [mytable] (
|
||||
[id] INTEGER PRIMARY KEY,
|
||||
[name] TEXT NOT NULL,
|
||||
[age] INTEGER NOT NULL,
|
||||
[is_good] INTEGER DEFAULT '1'
|
||||
mytable CREATE TABLE "mytable" (
|
||||
"id" INTEGER PRIMARY KEY,
|
||||
"name" TEXT NOT NULL,
|
||||
"age" INTEGER NOT NULL,
|
||||
"is_good" INTEGER DEFAULT '1'
|
||||
)
|
||||
|
||||
You can specify foreign key relationships between the tables you are creating using ``--fk colname othertable othercolumn``:
|
||||
|
|
@ -1964,14 +1964,14 @@ You can specify foreign key relationships between the tables you are creating us
|
|||
|
||||
table schema
|
||||
------- -------------------------------------------------
|
||||
authors CREATE TABLE [authors] (
|
||||
[id] INTEGER PRIMARY KEY,
|
||||
[name] TEXT
|
||||
authors CREATE TABLE "authors" (
|
||||
"id" INTEGER PRIMARY KEY,
|
||||
"name" TEXT
|
||||
)
|
||||
books CREATE TABLE [books] (
|
||||
[id] INTEGER PRIMARY KEY,
|
||||
[title] TEXT,
|
||||
[author_id] INTEGER REFERENCES [authors]([id])
|
||||
books CREATE TABLE "books" (
|
||||
"id" INTEGER PRIMARY KEY,
|
||||
"title" TEXT,
|
||||
"author_id" INTEGER REFERENCES "authors"("id")
|
||||
)
|
||||
|
||||
You can create a table in `SQLite STRICT mode <https://www.sqlite.org/stricttables.html>`__ using ``--strict``:
|
||||
|
|
@ -1988,9 +1988,9 @@ You can create a table in `SQLite STRICT mode <https://www.sqlite.org/stricttabl
|
|||
|
||||
table schema
|
||||
------- ------------------------
|
||||
mytable CREATE TABLE [mytable] (
|
||||
[id] INTEGER,
|
||||
[name] TEXT
|
||||
mytable CREATE TABLE "mytable" (
|
||||
"id" INTEGER,
|
||||
"name" TEXT
|
||||
) STRICT
|
||||
|
||||
If a table with the same name already exists, you will get an error. You can choose to silently ignore this error with ``--ignore``, or you can replace the existing table with a new, empty table using ``--replace``.
|
||||
|
|
@ -2100,16 +2100,16 @@ If you want to see the SQL that will be executed to make the change without actu
|
|||
|
||||
.. code-block:: output
|
||||
|
||||
CREATE TABLE [roadside_attractions_new_4033a60276b9] (
|
||||
[id] INTEGER PRIMARY KEY,
|
||||
[longitude] FLOAT,
|
||||
[latitude] FLOAT,
|
||||
[name] TEXT DEFAULT 'Untitled'
|
||||
CREATE TABLE "roadside_attractions_new_4033a60276b9" (
|
||||
"id" INTEGER PRIMARY KEY,
|
||||
"longitude" FLOAT,
|
||||
"latitude" FLOAT,
|
||||
"name" TEXT DEFAULT 'Untitled'
|
||||
);
|
||||
INSERT INTO [roadside_attractions_new_4033a60276b9] ([longitude], [latitude], [id], [name])
|
||||
SELECT [longitude], [latitude], [pk], [name] FROM [roadside_attractions];
|
||||
DROP TABLE [roadside_attractions];
|
||||
ALTER TABLE [roadside_attractions_new_4033a60276b9] RENAME TO [roadside_attractions];
|
||||
INSERT INTO "roadside_attractions_new_4033a60276b9" ("longitude", "latitude", "id", "name")
|
||||
SELECT "longitude", "latitude", "pk", "name" FROM "roadside_attractions";
|
||||
DROP TABLE "roadside_attractions";
|
||||
ALTER TABLE "roadside_attractions_new_4033a60276b9" RENAME TO "roadside_attractions";
|
||||
|
||||
.. _cli_transform_table_add_primary_key_to_rowid:
|
||||
|
||||
|
|
@ -2126,8 +2126,8 @@ SQLite tables that are created without an explicit primary key are created as `r
|
|||
|
||||
.. code-block:: output
|
||||
|
||||
CREATE TABLE [chickens] (
|
||||
[name] TEXT
|
||||
CREATE TABLE "chickens" (
|
||||
"name" TEXT
|
||||
);
|
||||
|
||||
.. code-block:: bash
|
||||
|
|
@ -2161,8 +2161,8 @@ You can use ``sqlite-utils transform ... --pk id`` to add a primary key column c
|
|||
.. code-block:: output
|
||||
|
||||
CREATE TABLE "chickens" (
|
||||
[id] INTEGER PRIMARY KEY,
|
||||
[name] TEXT
|
||||
"id" INTEGER PRIMARY KEY,
|
||||
"name" TEXT
|
||||
);
|
||||
|
||||
.. code-block:: bash
|
||||
|
|
@ -2208,17 +2208,17 @@ This would produce 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,
|
||||
[species] TEXT
|
||||
CREATE TABLE "species" (
|
||||
"id" INTEGER PRIMARY KEY,
|
||||
"species" TEXT
|
||||
);
|
||||
CREATE UNIQUE INDEX [idx_species_species]
|
||||
ON [species] ([species]);
|
||||
CREATE UNIQUE INDEX "idx_species_species"
|
||||
ON "species" ("species");
|
||||
|
||||
The command takes the following options:
|
||||
|
||||
|
|
@ -2251,40 +2251,40 @@ After running the above, the command ``sqlite-utils schema global.db`` reveals t
|
|||
|
||||
.. code-block:: sql
|
||||
|
||||
CREATE TABLE [countries] (
|
||||
[id] INTEGER PRIMARY KEY,
|
||||
[country] TEXT,
|
||||
[name] TEXT
|
||||
CREATE TABLE "countries" (
|
||||
"id" INTEGER PRIMARY KEY,
|
||||
"country" TEXT,
|
||||
"name" TEXT
|
||||
);
|
||||
CREATE TABLE "power_plants" (
|
||||
[country_id] INTEGER,
|
||||
[name] TEXT,
|
||||
[gppd_idnr] TEXT,
|
||||
[capacity_mw] TEXT,
|
||||
[latitude] TEXT,
|
||||
[longitude] TEXT,
|
||||
[primary_fuel] TEXT,
|
||||
[other_fuel1] TEXT,
|
||||
[other_fuel2] TEXT,
|
||||
[other_fuel3] TEXT,
|
||||
[commissioning_year] TEXT,
|
||||
[owner] TEXT,
|
||||
[source] TEXT,
|
||||
[url] TEXT,
|
||||
[geolocation_source] TEXT,
|
||||
[wepp_id] TEXT,
|
||||
[year_of_capacity_data] TEXT,
|
||||
[generation_gwh_2013] TEXT,
|
||||
[generation_gwh_2014] TEXT,
|
||||
[generation_gwh_2015] TEXT,
|
||||
[generation_gwh_2016] TEXT,
|
||||
[generation_gwh_2017] TEXT,
|
||||
[generation_data_source] TEXT,
|
||||
[estimated_generation_gwh] TEXT,
|
||||
FOREIGN KEY([country_id]) REFERENCES [countries]([id])
|
||||
"country_id" INTEGER,
|
||||
"name" TEXT,
|
||||
"gppd_idnr" TEXT,
|
||||
"capacity_mw" TEXT,
|
||||
"latitude" TEXT,
|
||||
"longitude" TEXT,
|
||||
"primary_fuel" TEXT,
|
||||
"other_fuel1" TEXT,
|
||||
"other_fuel2" TEXT,
|
||||
"other_fuel3" TEXT,
|
||||
"commissioning_year" TEXT,
|
||||
"owner" TEXT,
|
||||
"source" TEXT,
|
||||
"url" TEXT,
|
||||
"geolocation_source" TEXT,
|
||||
"wepp_id" TEXT,
|
||||
"year_of_capacity_data" TEXT,
|
||||
"generation_gwh_2013" TEXT,
|
||||
"generation_gwh_2014" TEXT,
|
||||
"generation_gwh_2015" TEXT,
|
||||
"generation_gwh_2016" TEXT,
|
||||
"generation_gwh_2017" TEXT,
|
||||
"generation_data_source" TEXT,
|
||||
"estimated_generation_gwh" TEXT,
|
||||
FOREIGN KEY("country_id") REFERENCES "countries"("id")
|
||||
);
|
||||
CREATE UNIQUE INDEX [idx_countries_country_name]
|
||||
ON [countries] ([country], [name]);
|
||||
CREATE UNIQUE INDEX "idx_countries_country_name"
|
||||
ON "countries" ("country", "name");
|
||||
|
||||
.. _cli_create_view:
|
||||
|
||||
|
|
@ -2567,17 +2567,17 @@ Use the ``--sql`` option to output the SQL that would be executed, rather than r
|
|||
select
|
||||
rowid,
|
||||
*
|
||||
from [documents]
|
||||
from "documents"
|
||||
)
|
||||
select
|
||||
[original].*
|
||||
"original".*
|
||||
from
|
||||
[original]
|
||||
join [documents_fts] on [original].rowid = [documents_fts].rowid
|
||||
"original"
|
||||
join "documents_fts" on "original".rowid = "documents_fts".rowid
|
||||
where
|
||||
[documents_fts] match :query
|
||||
"documents_fts" match :query
|
||||
order by
|
||||
[documents_fts].rank
|
||||
"documents_fts".rank
|
||||
|
||||
.. _cli_enable_counts:
|
||||
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
@ -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:
|
||||
|
|
@ -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
|
||||
# )
|
||||
|
||||
|
||||
|
|
@ -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:
|
||||
|
|
@ -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()``:
|
||||
|
|
@ -1665,9 +1665,9 @@ 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)
|
||||
)
|
||||
|
||||
|
|
@ -1675,9 +1675,9 @@ 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``.
|
||||
|
|
@ -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:
|
||||
|
|
@ -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:
|
||||
|
|
@ -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:
|
||||
|
|
@ -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:
|
||||
|
|
@ -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:
|
||||
|
||||
|
|
@ -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:
|
||||
|
|
@ -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:
|
||||
|
|
@ -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:
|
||||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
]
|
||||
}
|
||||
|
|
@ -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"
|
||||
]
|
||||
}
|
||||
|
|
@ -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"
|
||||
]
|
||||
|
|
@ -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"
|
||||
]
|
||||
|
|
@ -1048,4 +1048,4 @@
|
|||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue