mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-24 19:04:12 +02:00
add_column now accepts explicit SQLite types, refs #15
This commit is contained in:
parent
c5286d2c66
commit
6d25f648ed
3 changed files with 25 additions and 1 deletions
|
|
@ -190,9 +190,16 @@ You can add a new column to a table using the ``.add_column(col_name, col_type)`
|
||||||
|
|
||||||
.. code-block:: python
|
.. 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("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"
|
float: "FLOAT"
|
||||||
int: "INTEGER"
|
int: "INTEGER"
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,15 @@ COLUMN_TYPE_MAPPING = {
|
||||||
datetime.date: "TEXT",
|
datetime.date: "TEXT",
|
||||||
datetime.time: "TEXT",
|
datetime.time: "TEXT",
|
||||||
None.__class__: "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 numpy is available, add more types
|
||||||
if np:
|
if np:
|
||||||
|
|
|
||||||
|
|
@ -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)"),
|
("dob", datetime.date, "CREATE TABLE [dogs] ( [name] TEXT , [dob] TEXT)"),
|
||||||
("age", int, "CREATE TABLE [dogs] ( [name] TEXT , [age] INTEGER)"),
|
("age", int, "CREATE TABLE [dogs] ( [name] TEXT , [age] INTEGER)"),
|
||||||
("weight", float, "CREATE TABLE [dogs] ( [name] TEXT , [weight] FLOAT)"),
|
("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):
|
def test_add_column(fresh_db, col_name, col_type, expected_schema):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue