mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-10-08 02:17:08 +02:00
parent
e878f2a8fe
commit
de10590344
3 changed files with 27 additions and 1 deletions
|
|
@ -37,6 +37,12 @@ If you want to create an in-memory database, you can do so like this:
|
||||||
|
|
||||||
db = Database(memory=True)
|
db = Database(memory=True)
|
||||||
|
|
||||||
|
Connections use ``PRAGMA recursive_triggers=on`` by default. If you don't want to use `recursive triggers <https://www.sqlite.org/pragma.html#pragma_recursive_triggers>`__ you can turn them off using:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
db = Database(memory=True, recursive_triggers=False)
|
||||||
|
|
||||||
Tables are accessed using the indexing operator, like so:
|
Tables are accessed using the indexing operator, like so:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
|
||||||
|
|
@ -103,7 +103,13 @@ class PrimaryKeyRequired(Exception):
|
||||||
|
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
def __init__(self, filename_or_conn=None, memory=False, recreate=False):
|
def __init__(
|
||||||
|
self,
|
||||||
|
filename_or_conn=None,
|
||||||
|
memory=False,
|
||||||
|
recreate=False,
|
||||||
|
recursive_triggers=True,
|
||||||
|
):
|
||||||
assert (filename_or_conn is not None and not memory) or (
|
assert (filename_or_conn is not None and not memory) or (
|
||||||
filename_or_conn is None and memory
|
filename_or_conn is None and memory
|
||||||
), "Either specify a filename_or_conn or pass memory=True"
|
), "Either specify a filename_or_conn or pass memory=True"
|
||||||
|
|
@ -116,6 +122,8 @@ class Database:
|
||||||
else:
|
else:
|
||||||
assert not recreate, "recreate cannot be used with connections, only paths"
|
assert not recreate, "recreate cannot be used with connections, only paths"
|
||||||
self.conn = filename_or_conn
|
self.conn = filename_or_conn
|
||||||
|
if recursive_triggers:
|
||||||
|
self.conn.execute("PRAGMA recursive_triggers=on;")
|
||||||
|
|
||||||
def __getitem__(self, table_name):
|
def __getitem__(self, table_name):
|
||||||
return self.table(table_name)
|
return self.table(table_name)
|
||||||
|
|
|
||||||
12
tests/test_constructor.py
Normal file
12
tests/test_constructor.py
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
from sqlite_utils import Database
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def test_recursive_triggers():
|
||||||
|
db = Database(memory=True)
|
||||||
|
assert db.conn.execute("PRAGMA recursive_triggers").fetchone()[0]
|
||||||
|
|
||||||
|
|
||||||
|
def test_recursive_triggers_off():
|
||||||
|
db = Database(memory=True, recursive_triggers=False)
|
||||||
|
assert not db.conn.execute("PRAGMA recursive_triggers").fetchone()[0]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue