Skip to content

Commit fb93452

Browse files
authored
Use double quotes not braces for tables and columns (#678)
Closes #677
1 parent bf1ac77 commit fb93452

20 files changed

+910
-849
lines changed

docs/cli.rst

Lines changed: 136 additions & 136 deletions
Large diffs are not rendered by default.

docs/python-api.rst

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,8 @@ The ``db.schema`` property returns the full SQL schema for the database as a str
459459
>>> db = sqlite_utils.Database("dogs.db")
460460
>>> print(db.schema)
461461
CREATE TABLE "dogs" (
462-
[id] INTEGER PRIMARY KEY,
463-
[name] TEXT
462+
"id" INTEGER PRIMARY KEY,
463+
"name" TEXT
464464
);
465465

466466
.. _python_api_creating_tables:
@@ -544,11 +544,11 @@ This will create a table with the following schema:
544544

545545
.. code-block:: sql
546546
547-
CREATE TABLE [dogs] (
548-
[id] INTEGER PRIMARY KEY,
549-
[name] TEXT,
550-
[age] INTEGER,
551-
[weight] FLOAT
547+
CREATE TABLE "dogs" (
548+
"id" INTEGER PRIMARY KEY,
549+
"name" TEXT,
550+
"age" INTEGER,
551+
"weight" FLOAT
552552
)
553553
554554
.. _python_api_explicit_create:
@@ -729,10 +729,10 @@ Here's an example that uses these features:
729729
# {'id': 3, 'name': 'Dharma', 'score': 1}]
730730
print(db.table("authors").schema)
731731
# Outputs:
732-
# CREATE TABLE [authors] (
733-
# [id] INTEGER PRIMARY KEY,
734-
# [name] TEXT NOT NULL,
735-
# [score] INTEGER NOT NULL DEFAULT 1
732+
# CREATE TABLE "authors" (
733+
# "id" INTEGER PRIMARY KEY,
734+
# "name" TEXT NOT NULL,
735+
# "score" INTEGER NOT NULL DEFAULT 1
736736
# )
737737
738738
@@ -1196,10 +1196,10 @@ You can inspect the database to see the results like this::
11961196
>>> list(db.table("characteristics_dogs").rows)
11971197
[{'characteristics_id': 1, 'dogs_id': 1}, {'characteristics_id': 2, 'dogs_id': 1}]
11981198
>>> print(db.table("characteristics_dogs").schema)
1199-
CREATE TABLE [characteristics_dogs] (
1200-
[characteristics_id] INTEGER REFERENCES [characteristics]([id]),
1201-
[dogs_id] INTEGER REFERENCES [dogs]([id]),
1202-
PRIMARY KEY ([characteristics_id], [dogs_id])
1199+
CREATE TABLE "characteristics_dogs" (
1200+
"characteristics_id" INTEGER REFERENCES "characteristics"("id"),
1201+
"dogs_id" INTEGER REFERENCES "dogs"("id"),
1202+
PRIMARY KEY ("characteristics_id", "dogs_id")
12031203
)
12041204

12051205
.. _python_api_analyze_column:
@@ -1648,10 +1648,10 @@ The schema of the above table is:
16481648
16491649
.. code-block:: sql
16501650
1651-
CREATE TABLE [Trees] (
1652-
[id] INTEGER PRIMARY KEY,
1653-
[TreeAddress] TEXT,
1654-
[Species] TEXT
1651+
CREATE TABLE "Trees" (
1652+
"id" INTEGER PRIMARY KEY,
1653+
"TreeAddress" TEXT,
1654+
"Species" TEXT
16551655
)
16561656
16571657
Here's how to extract the ``Species`` column using ``.extract()``:
@@ -1665,19 +1665,19 @@ After running this code the table schema now looks like this:
16651665
.. code-block:: sql
16661666
16671667
CREATE TABLE "Trees" (
1668-
[id] INTEGER PRIMARY KEY,
1669-
[TreeAddress] TEXT,
1670-
[Species_id] INTEGER,
1668+
"id" INTEGER PRIMARY KEY,
1669+
"TreeAddress" TEXT,
1670+
"Species_id" INTEGER,
16711671
FOREIGN KEY(Species_id) REFERENCES Species(id)
16721672
)
16731673
16741674
A new ``Species`` table will have been created with the following schema:
16751675
16761676
.. code-block:: sql
16771677
1678-
CREATE TABLE [Species] (
1679-
[id] INTEGER PRIMARY KEY,
1680-
[Species] TEXT
1678+
CREATE TABLE "Species" (
1679+
"id" INTEGER PRIMARY KEY,
1680+
"Species" TEXT
16811681
)
16821682
16831683
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:
16931693
.. code-block:: sql
16941694
16951695
CREATE TABLE "Trees" (
1696-
[id] INTEGER PRIMARY KEY,
1697-
[TreeAddress] TEXT,
1698-
[tree_species_id] INTEGER,
1696+
"id" INTEGER PRIMARY KEY,
1697+
"TreeAddress" TEXT,
1698+
"tree_species_id" INTEGER,
16991699
FOREIGN KEY(tree_species_id) REFERENCES tree_species(id)
17001700
)
17011701
1702-
CREATE TABLE [tree_species] (
1703-
[id] INTEGER PRIMARY KEY,
1704-
[Species] TEXT
1702+
CREATE TABLE "tree_species" (
1703+
"id" INTEGER PRIMARY KEY,
1704+
"Species" TEXT
17051705
)
17061706
17071707
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:
17261726
.. code-block:: sql
17271727
17281728
CREATE TABLE "Trees" (
1729-
[id] INTEGER PRIMARY KEY,
1730-
[TreeAddress] TEXT,
1731-
[CommonName_LatinName_id] INTEGER,
1729+
"id" INTEGER PRIMARY KEY,
1730+
"TreeAddress" TEXT,
1731+
"CommonName_LatinName_id" INTEGER,
17321732
FOREIGN KEY(CommonName_LatinName_id) REFERENCES CommonName_LatinName(id)
17331733
)
1734-
CREATE TABLE [CommonName_LatinName] (
1735-
[id] INTEGER PRIMARY KEY,
1736-
[CommonName] TEXT,
1737-
[LatinName] TEXT
1734+
CREATE TABLE "CommonName_LatinName" (
1735+
"id" INTEGER PRIMARY KEY,
1736+
"CommonName" TEXT,
1737+
"LatinName" TEXT
17381738
)
17391739
17401740
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:
17481748
.. code-block:: sql
17491749
17501750
CREATE TABLE "Trees" (
1751-
[id] INTEGER PRIMARY KEY,
1752-
[TreeAddress] TEXT,
1753-
[species_id] INTEGER,
1751+
"id" INTEGER PRIMARY KEY,
1752+
"TreeAddress" TEXT,
1753+
"species_id" INTEGER,
17541754
FOREIGN KEY(species_id) REFERENCES Species(id)
17551755
)
1756-
CREATE TABLE [Species] (
1757-
[id] INTEGER PRIMARY KEY,
1758-
[CommonName] TEXT,
1759-
[LatinName] TEXT
1756+
CREATE TABLE "Species" (
1757+
"id" INTEGER PRIMARY KEY,
1758+
"CommonName" TEXT,
1759+
"LatinName" TEXT
17601760
)
17611761
17621762
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:
17741774
17751775
.. code-block:: sql
17761776
1777-
CREATE TABLE [Species] (
1778-
[id] INTEGER PRIMARY KEY,
1779-
[name] TEXT,
1780-
[latin] TEXT
1777+
CREATE TABLE "Species" (
1778+
"id" INTEGER PRIMARY KEY,
1779+
"name" TEXT,
1780+
"latin" TEXT
17811781
)
17821782
17831783
.. _python_api_hash:
@@ -2100,12 +2100,12 @@ The ``.schema`` property outputs the table's schema as a SQL string::
21002100
"Longitude" REAL,
21012101
"Location" TEXT
21022102
,
2103-
FOREIGN KEY ("PlantType") REFERENCES [PlantType](id),
2104-
FOREIGN KEY ("qCaretaker") REFERENCES [qCaretaker](id),
2105-
FOREIGN KEY ("qSpecies") REFERENCES [qSpecies](id),
2106-
FOREIGN KEY ("qSiteInfo") REFERENCES [qSiteInfo](id),
2107-
FOREIGN KEY ("qCareAssistant") REFERENCES [qCareAssistant](id),
2108-
FOREIGN KEY ("qLegalStatus") REFERENCES [qLegalStatus](id))
2103+
FOREIGN KEY ("PlantType") REFERENCES "PlantType"(id),
2104+
FOREIGN KEY ("qCaretaker") REFERENCES "qCaretaker"(id),
2105+
FOREIGN KEY ("qSpecies") REFERENCES "qSpecies"(id),
2106+
FOREIGN KEY ("qSiteInfo") REFERENCES "qSiteInfo"(id),
2107+
FOREIGN KEY ("qCareAssistant") REFERENCES "qCareAssistant"(id),
2108+
FOREIGN KEY ("qLegalStatus") REFERENCES "qLegalStatus"(id))
21092109
21102110
.. _python_api_introspection_strict:
21112111
@@ -2507,9 +2507,9 @@ This will create the ``_counts`` table if it does not already exist, with the fo
25072507
25082508
.. code-block:: sql
25092509
2510-
CREATE TABLE [_counts] (
2511-
[table] TEXT PRIMARY KEY,
2512-
[count] INTEGER DEFAULT 0
2510+
CREATE TABLE "_counts" (
2511+
"table" TEXT PRIMARY KEY,
2512+
"count" INTEGER DEFAULT 0
25132513
)
25142514
25152515
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:
27192719
27202720
# The table schema looks like this:
27212721
# print(db.table("cats").schema)
2722-
# CREATE TABLE [cats] (
2723-
# [id] INTEGER PRIMARY KEY,
2724-
# [name] TEXT,
2725-
# [age] INTEGER,
2726-
# [thumbnail] BLOB
2722+
# CREATE TABLE "cats" (
2723+
# "id" INTEGER PRIMARY KEY,
2724+
# "name" TEXT,
2725+
# "age" INTEGER,
2726+
# "thumbnail" BLOB
27272727
# )
27282728
27292729
.. _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
28872887
db.table("creatures").insert_all(rows)
28882888
print(db.schema)
28892889
# Outputs:
2890-
# CREATE TABLE [creatures] (
2891-
# [id] TEXT,
2892-
# [name] TEXT
2890+
# CREATE TABLE "creatures" (
2891+
# "id" TEXT,
2892+
# "name" TEXT
28932893
# );
28942894
28952895
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()
29102910
db.table("creatures2").transform(types=tracker.types)
29112911
print(db.table("creatures2").schema)
29122912
# Outputs:
2913-
# CREATE TABLE [creatures2] (
2914-
# [id] INTEGER,
2915-
# [name] TEXT
2913+
# CREATE TABLE "creatures2" (
2914+
# "id" INTEGER,
2915+
# "name" TEXT
29162916
# );
29172917
29182918
.. _python_api_gis:

docs/tutorial.ipynb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@
180180
"name": "stdout",
181181
"output_type": "stream",
182182
"text": [
183-
"CREATE TABLE [creatures] (\n",
184-
" [name] TEXT,\n",
185-
" [species] TEXT,\n",
186-
" [age] FLOAT\n",
183+
"CREATE TABLE \"creatures\" (\n",
184+
" \"name\" TEXT,\n",
185+
" \"species\" TEXT,\n",
186+
" \"age\" FLOAT\n",
187187
")\n"
188188
]
189189
}
@@ -534,11 +534,11 @@
534534
"name": "stdout",
535535
"output_type": "stream",
536536
"text": [
537-
"CREATE TABLE [creatures] (\n",
538-
" [id] INTEGER PRIMARY KEY,\n",
539-
" [name] TEXT,\n",
540-
" [species] TEXT,\n",
541-
" [age] FLOAT\n",
537+
"CREATE TABLE \"creatures\" (\n",
538+
" \"id\" INTEGER PRIMARY KEY,\n",
539+
" \"name\" TEXT,\n",
540+
" \"species\" TEXT,\n",
541+
" \"age\" FLOAT\n",
542542
")\n"
543543
]
544544
}
@@ -929,11 +929,11 @@
929929
"output_type": "stream",
930930
"text": [
931931
"CREATE TABLE \"creatures\" (\n",
932-
" [id] INTEGER PRIMARY KEY,\n",
933-
" [name] TEXT,\n",
934-
" [species_id] INTEGER,\n",
935-
" [age] FLOAT,\n",
936-
" FOREIGN KEY([species_id]) REFERENCES [species]([id])\n",
932+
" \"id\" INTEGER PRIMARY KEY,\n",
933+
" \"name\" TEXT,\n",
934+
" \"species_id\" INTEGER,\n",
935+
" \"age\" FLOAT,\n",
936+
" FOREIGN KEY(\"species_id\") REFERENCES \"species\"(\"id\")\n",
937937
")\n",
938938
"[{'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"
939939
]
@@ -962,9 +962,9 @@
962962
"name": "stdout",
963963
"output_type": "stream",
964964
"text": [
965-
"CREATE TABLE [species] (\n",
966-
" [id] INTEGER PRIMARY KEY,\n",
967-
" [species] TEXT\n",
965+
"CREATE TABLE \"species\" (\n",
966+
" \"id\" INTEGER PRIMARY KEY,\n",
967+
" \"species\" TEXT\n",
968968
")\n",
969969
"[{'id': 1, 'species': 'dog'}, {'id': 2, 'species': 'chicken'}]\n"
970970
]
@@ -1048,4 +1048,4 @@
10481048
},
10491049
"nbformat": 4,
10501050
"nbformat_minor": 5
1051-
}
1051+
}

sqlite_utils/cli.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
import pathlib
77
from runpy import run_module
88
import sqlite_utils
9-
from sqlite_utils.db import AlterError, BadMultiValues, DescIndex, NoTable
9+
from sqlite_utils.db import (
10+
AlterError,
11+
BadMultiValues,
12+
DescIndex,
13+
NoTable,
14+
quote_identifier,
15+
)
1016
from sqlite_utils.plugins import pm, get_plugins
1117
from sqlite_utils.utils import maximize_csv_field_size_limit
1218
from sqlite_utils import recipes
@@ -1967,7 +1973,9 @@ def memory(
19671973
view_names.append("t")
19681974
for view_name in view_names:
19691975
if not db[view_name].exists():
1970-
db.create_view(view_name, "select * from [{}]".format(file_table))
1976+
db.create_view(
1977+
view_name, "select * from {}".format(quote_identifier(file_table))
1978+
)
19711979

19721980
if fp:
19731981
fp.close()
@@ -2230,8 +2238,8 @@ def rows(
22302238
"""
22312239
columns = "*"
22322240
if column:
2233-
columns = ", ".join("[{}]".format(c) for c in column)
2234-
sql = "select {} from [{}]".format(columns, dbtable)
2241+
columns = ", ".join(quote_identifier(c) for c in column)
2242+
sql = "select {} from {}".format(columns, quote_identifier(dbtable))
22352243
if where:
22362244
sql += " where " + where
22372245
if order:
@@ -2288,10 +2296,10 @@ def triggers(
22882296
\b
22892297
sqlite-utils triggers trees.db
22902298
"""
2291-
sql = "select name, tbl_name as [table], sql from sqlite_master where type = 'trigger'"
2299+
sql = "select name, tbl_name as \"table\", sql from sqlite_master where type = 'trigger'"
22922300
if tables:
22932301
quote = sqlite_utils.Database(memory=True).quote
2294-
sql += " and [table] in ({})".format(
2302+
sql += ' and "table" in ({})'.format(
22952303
", ".join(quote(table) for table in tables)
22962304
)
22972305
ctx.invoke(

0 commit comments

Comments
 (0)