mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-28 04:44:26 +02:00
Database() constructor works with pathlib.Path
This commit is contained in:
parent
3f2e711a4b
commit
57fc8ce55e
2 changed files with 12 additions and 1 deletions
|
|
@ -2,6 +2,7 @@ import sqlite3
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
|
import pathlib
|
||||||
|
|
||||||
Column = namedtuple(
|
Column = namedtuple(
|
||||||
"Column", ("cid", "name", "type", "notnull", "default_value", "is_pk")
|
"Column", ("cid", "name", "type", "notnull", "default_value", "is_pk")
|
||||||
|
|
@ -16,6 +17,8 @@ class Database:
|
||||||
def __init__(self, filename_or_conn):
|
def __init__(self, filename_or_conn):
|
||||||
if isinstance(filename_or_conn, str):
|
if isinstance(filename_or_conn, str):
|
||||||
self.conn = sqlite3.connect(filename_or_conn)
|
self.conn = sqlite3.connect(filename_or_conn)
|
||||||
|
elif isinstance(filename_or_conn, pathlib.Path):
|
||||||
|
self.conn = sqlite3.connect(str(filename_or_conn))
|
||||||
else:
|
else:
|
||||||
self.conn = filename_or_conn
|
self.conn = filename_or_conn
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
from sqlite_utils.db import Index
|
from sqlite_utils.db import Index, Database
|
||||||
import collections
|
import collections
|
||||||
import datetime
|
import datetime
|
||||||
|
import pathlib
|
||||||
import pytest
|
import pytest
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
@ -181,3 +182,10 @@ def test_create_view(fresh_db):
|
||||||
def test_vacuum(fresh_db):
|
def test_vacuum(fresh_db):
|
||||||
fresh_db["data"].insert({"foo": "foo", "bar": "bar"})
|
fresh_db["data"].insert({"foo": "foo", "bar": "bar"})
|
||||||
fresh_db.vacuum()
|
fresh_db.vacuum()
|
||||||
|
|
||||||
|
|
||||||
|
def test_works_with_pathlib_path(tmpdir):
|
||||||
|
path = pathlib.Path(tmpdir / "test.db")
|
||||||
|
db = Database(path)
|
||||||
|
db["demo"].insert_all([{"foo": 1}])
|
||||||
|
assert 1 == db["demo"].count
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue