diff --git a/docs/python-api.rst b/docs/python-api.rst index fb060b4..9ae6646 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -190,9 +190,16 @@ You can add a new column to a table using the ``.add_column(col_name, col_type)` .. code-block:: python + db["dogs"].add_column("instagram", str) + db["dogs"].add_column("weight", float) db["dogs"].add_column("dob", datetime.date) + db["dogs"].add_column("image", "BLOB") -You can pass any of the following Python types as the second argument - ``sqlite-utils`` will create the column using the appropriate SQLite type from this list:: +You can specify the ``col_type`` argument either using a SQLite type as a string, or by directly passing a Python type e.g. ``str`` or ``float``. + +SQLite types you can specify are ``"TEXT"``, ``"INTEGER"``, ``"FLOAT"`` or ``"BLOB"``. + +If you pass a Python type, it will be mapped to SQLite types as shown here:: float: "FLOAT" int: "INTEGER" diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 7576141..c1bbd74 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -30,6 +30,15 @@ COLUMN_TYPE_MAPPING = { datetime.date: "TEXT", datetime.time: "TEXT", None.__class__: "TEXT", + # SQLite explicit types + "TEXT": "TEXT", + "INTEGER": "INTEGER", + "FLOAT": "FLOAT", + "BLOB": "BLOB", + "text": "TEXT", + "integer": "INTEGER", + "float": "FLOAT", + "blob": "BLOB", } # If numpy is available, add more types if np: diff --git a/tests/test_create.py b/tests/test_create.py index ddc45e5..aae4817 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -130,6 +130,14 @@ def test_create_table_works_for_m2m_with_only_foreign_keys(fresh_db): ("dob", datetime.date, "CREATE TABLE [dogs] ( [name] TEXT , [dob] TEXT)"), ("age", int, "CREATE TABLE [dogs] ( [name] TEXT , [age] INTEGER)"), ("weight", float, "CREATE TABLE [dogs] ( [name] TEXT , [weight] FLOAT)"), + ("text", "TEXT", "CREATE TABLE [dogs] ( [name] TEXT , [text] TEXT)"), + ( + "integer", + "INTEGER", + "CREATE TABLE [dogs] ( [name] TEXT , [integer] INTEGER)", + ), + ("float", "FLOAT", "CREATE TABLE [dogs] ( [name] TEXT , [float] FLOAT)"), + ("blob", "blob", "CREATE TABLE [dogs] ( [name] TEXT , [blob] BLOB)"), ), ) def test_add_column(fresh_db, col_name, col_type, expected_schema):