Skip to content

Commit 3b18b9e

Browse files
committed
Fix for REAL columns by CSV --detect-types
Refs #645 (comment)
1 parent 40cb853 commit 3b18b9e

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

docs/cli-reference.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ See :ref:`cli_add_column`.
11421142
::
11431143

11441144
Usage: sqlite-utils add-column [OPTIONS] PATH TABLE COL_NAME
1145-
[[integer|int|float|text|str|blob|bytes]]
1145+
[[integer|int|float|real|text|str|blob|bytes]]
11461146

11471147
Add a column to the specified table
11481148

sqlite_utils/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def dump(path, load_extension):
419419
@click.argument(
420420
"col_type",
421421
type=click.Choice(
422-
["integer", "int", "float", "text", "str", "blob", "bytes"],
422+
["integer", "int", "float", "real", "text", "str", "blob", "bytes"],
423423
case_sensitive=False,
424424
),
425425
required=False,

sqlite_utils/db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class Default:
216216
"str": "TEXT",
217217
"integer": "INTEGER",
218218
"int": "INTEGER",
219-
"float": "FLOAT",
219+
"float": "REAL",
220220
"real": "REAL",
221221
"blob": "BLOB",
222222
"bytes": "BLOB",

tests/test_cli.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def test_create_index_desc(db_path):
284284
"int",
285285
'CREATE TABLE "dogs" (\n "name" TEXT\n, "integer" INTEGER)',
286286
),
287-
("float", "FLOAT", 'CREATE TABLE "dogs" (\n "name" TEXT\n, "float" FLOAT)'),
287+
("float", "FLOAT", 'CREATE TABLE "dogs" (\n "name" TEXT\n, "float" REAL)'),
288288
("blob", "blob", 'CREATE TABLE "dogs" (\n "name" TEXT\n, "blob" BLOB)'),
289289
("blob", "BLOB", 'CREATE TABLE "dogs" (\n "name" TEXT\n, "blob" BLOB)'),
290290
("blob", "bytes", 'CREATE TABLE "dogs" (\n "name" TEXT\n, "blob" BLOB)'),
@@ -2240,6 +2240,28 @@ def test_upsert_detect_types(tmpdir, option):
22402240
]
22412241

22422242

2243+
def test_csv_detect_types_creates_real_columns(tmpdir):
2244+
"""Test that CSV import with --detect-types creates REAL columns for floats"""
2245+
db_path = str(tmpdir / "test.db")
2246+
data = "name,age,weight\nCleo,6,45.5\nDori,1,3.5"
2247+
result = CliRunner().invoke(
2248+
cli.cli,
2249+
["insert", db_path, "creatures", "-", "--csv", "--detect-types"],
2250+
catch_exceptions=False,
2251+
input=data,
2252+
)
2253+
assert result.exit_code == 0
2254+
db = Database(db_path)
2255+
# Check that the schema uses REAL for the weight column
2256+
assert db["creatures"].schema == (
2257+
'CREATE TABLE "creatures" (\n'
2258+
' "name" TEXT,\n'
2259+
' "age" INTEGER,\n'
2260+
' "weight" REAL\n'
2261+
")"
2262+
)
2263+
2264+
22432265
def test_integer_overflow_error(tmpdir):
22442266
db_path = str(tmpdir / "test.db")
22452267
result = CliRunner().invoke(

0 commit comments

Comments
 (0)