Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5501,6 +5501,9 @@ def drop(self, ignore: bool = False) -> None:
def jsonify_if_needed(value: object) -> object:
if isinstance(value, decimal.Decimal):
return float(value)
if np is not None and isinstance(value, (np.integer, np.floating)):
# sqlite3 would otherwise store the raw buffer as a BLOB
return cast(Any, value).item()
if isinstance(value, (dict, list, tuple)):
return json.dumps(value, default=repr, ensure_ascii=False)
elif isinstance(value, (datetime.time, datetime.date, datetime.datetime)):
Expand Down
39 changes: 39 additions & 0 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -1739,3 +1739,42 @@ def test_chained_create_sets_pks(fresh_db):
{"id": int, "name": str, "color": str}
)
assert table.pks == ["id"]


def test_insert_raw_numpy_scalars(fresh_db):
# https://github.com/simonw/sqlite-utils/issues/876
np = pytest.importorskip("numpy")
row = {
"int8": np.int8(-8),
"int64": np.int64(5),
"uint64": np.uint64(64),
"float16": np.float16(16.5),
"float32": np.float32(32.5),
"float64": np.float64(64.5),
}
table = fresh_db.table("t")
table.insert(row)
table.insert_all([row])
assert table.columns_dict == {
"int8": int,
"int64": int,
"uint64": int,
"float16": float,
"float32": float,
"float64": float,
}
expected = {
"int8": -8,
"int64": 5,
"uint64": 64,
"float16": 16.5,
"float32": 32.5,
"float64": 64.5,
}
assert list(table.rows) == [expected, expected]
assert fresh_db.execute(
"select typeof(int64), typeof(float32) from t"
).fetchall() == [
("integer", "real"),
("integer", "real"),
]
Loading