dblite  1.3.0
Simple query interface for SQL databases
dblite.engines.sqlite.Database Class Reference

Convenience wrapper around sqlite3.Connection. More...

Inheritance diagram for dblite.engines.sqlite.Database:
Inheritance graph

Public Member Functions

def __init__ (self, opts=":memory:", **kwargs)
 Creates a new Database instance for SQLite. More...
 
def __enter__ (self)
 Context manager entry, opens database if not already open, returns Database object. More...
 
def __exit__ (self, exc_type, exc_val, exc_trace)
 Context manager exit, closes database and any pending transactions if open. More...
 
def close (self, commit=None)
 Closes the database and any pending transactions, if open. More...
 
def execute (self, sql, args=())
 Executes the SQL statement and returns sqlite3.Cursor. More...
 
def executemany (self, sql, args)
 Executes the SQL statement against all parameter sequences. More...
 
def executescript (self, sql)
 Executes the SQL as script of any number of statements. More...
 
def open (self)
 Opens the database connection, if not already open. More...
 
def transaction (self, commit=True, exclusive=True, **kwargs)
 Returns a transaction context manager. More...
 
- Public Member Functions inherited from dblite.api.Database
def __del__ (self)
 Closes the database, if open. More...
 
- Public Member Functions inherited from dblite.api.Queryable
def delete (self, table, where=(), **kwargs)
 Convenience wrapper for database DELETE, returns affected row count. More...
 
def fetchall (self, table, cols="*", where=(), group=(), order=(), limit=(), **kwargs)
 Convenience wrapper for database SELECT and fetch all. More...
 
def fetchone (self, table, cols="*", where=(), group=(), order=(), limit=(), **kwargs)
 Convenience wrapper for database SELECT and fetch one. More...
 
def select (self, table, cols="*", where=(), group=(), order=(), limit=(), **kwargs)
 Convenience wrapper for database SELECT, returns database cursor. More...
 
def update (self, table, values, where=(), **kwargs)
 Convenience wrapper for database UPDATE, returns affected row count. More...
 
- Public Member Functions inherited from dblite.engines.sqlite.Queryable
def insert (self, table, values=(), **kwargs)
 Convenience wrapper for database INSERT, returns inserted row ID. More...
 
def insertmany (self, table, rows=(), **kwargs)
 Convenience wrapper for database multiple INSERTs, returns list of inserted row IDs. More...
 
def makeSQL (self, action, table, cols="*", where=(), group=(), order=(), limit=(), values=(), kwargs=None)
 Returns (SQL statement string, parameter dict). More...
 
def quote (cls, value, force=False)
 Returns identifier in quotes and proper-escaped for queries, if value needs quoting (has non-alphanumerics, starts with number, or is reserved). More...
 

Public Attributes

 connection
 
 path
 

Static Public Attributes

 MUTEX = collections.defaultdict(threading.RLock)
 Mutexes for exclusive actions, as {Database instance: lock}. More...
 
 ROW_FACTORY = None
 Registered row factory. More...
 
- Static Public Attributes inherited from dblite.api.Queryable
 ENGINE = None
 Underlying database engine, "sqlite" for SQLite3 and "postgres" for PostgreSQL. More...
 
- Static Public Attributes inherited from dblite.engines.sqlite.Queryable
string ENGINE = "sqlite"
 Name of underlying database engine. More...
 
list OPS
 Recognized binary operators for makeSQL() More...
 

Properties

 closed = property
 Whether database is currently not open. More...
 
 cursor = property
 Database engine cursor object, or `None` if closed. More...
 
 row_factory = property
 The custom row factory, if any, as `function(cursor, row tuple)`. More...
 
- Properties inherited from dblite.api.Database
 row_factory = property
 The custom row factory, if any, as `function(cursor, row tuple)`. More...
 
- Properties inherited from dblite.api.Queryable
 closed = property
 Whether currently not open. More...
 
 cursor = property
 Database engine cursor object, or `None` if closed. More...
 

Detailed Description

Convenience wrapper around sqlite3.Connection.

Queries directly on the Database object use autocommit mode.

Definition at line 247 of file sqlite.py.

Constructor & Destructor Documentation

◆ __init__()

def dblite.engines.sqlite.Database.__init__ (   self,
  opts = ":memory:",
**  kwargs 
)

Creates a new Database instance for SQLite.

Parameters
optsfile path or `":memory:"`
kwargssupported arguments are passed to sqlite3.connect() in open(), like `detect_types=sqlite3.PARSE_COLNAMES`

Definition at line 262 of file sqlite.py.

Member Function Documentation

◆ __enter__()

def dblite.engines.sqlite.Database.__enter__ (   self)

Context manager entry, opens database if not already open, returns Database object.

Reimplemented from dblite.api.Database.

Definition at line 274 of file sqlite.py.

◆ __exit__()

def dblite.engines.sqlite.Database.__exit__ (   self,
  exc_type,
  exc_val,
  exc_trace 
)

Context manager exit, closes database and any pending transactions if open.

Reimplemented from dblite.api.Database.

Definition at line 280 of file sqlite.py.

◆ close()

def dblite.engines.sqlite.Database.close (   self,
  commit = None 
)

Closes the database and any pending transactions, if open.

Parameters
commit`True` for explicit commit on open transactions, `False` for explicit rollback on open transactions, `None` defaults to `commit` flag from transaction creations

Reimplemented from dblite.api.Database.

Definition at line 341 of file sqlite.py.

◆ execute()

def dblite.engines.sqlite.Database.execute (   self,
  sql,
  args = () 
)

Executes the SQL statement and returns sqlite3.Cursor.

Parameters
sqlSQL statement to execute, with SQLite-specific parameter bindings, if any
argsdictionary for :name placeholders, or a sequence for positional ? placeholders

Reimplemented from dblite.api.Queryable.

Definition at line 294 of file sqlite.py.

◆ executemany()

def dblite.engines.sqlite.Database.executemany (   self,
  sql,
  args 
)

Executes the SQL statement against all parameter sequences.

Parameters
sqlSQL statement to execute, with SQLite-specific parameter bindings
argsiterable of query parameters, as dictionaries for :name placeholders or sequences for positional ? placeholders

Reimplemented from dblite.api.Queryable.

Definition at line 304 of file sqlite.py.

◆ executescript()

def dblite.engines.sqlite.Database.executescript (   self,
  sql 
)

Executes the SQL as script of any number of statements.

Parameters
sqlscript with one or more SQL statements

Reimplemented from dblite.api.Queryable.

Definition at line 313 of file sqlite.py.

◆ open()

def dblite.engines.sqlite.Database.open (   self)

Opens the database connection, if not already open.

Reimplemented from dblite.api.Database.

Definition at line 317 of file sqlite.py.

◆ transaction()

def dblite.engines.sqlite.Database.transaction (   self,
  commit = True,
  exclusive = True,
**  kwargs 
)

Returns a transaction context manager.

Context is breakable by raising Rollback.

Parameters
commitwhether transaction commits automatically at exiting with-block
exclusivewhether entering a with-block is exclusive over other Transaction instances on this Database
kwargsengine-specific arguments, like `detect_types=sqlite3.PARSE_COLNAMES`

Reimplemented from dblite.api.Database.

Definition at line 396 of file sqlite.py.

Member Data Documentation

◆ connection

dblite.engines.sqlite.Database.connection

Definition at line 264 of file sqlite.py.

◆ MUTEX

dblite.engines.sqlite.Database.MUTEX = collections.defaultdict(threading.RLock)
static

Mutexes for exclusive actions, as {Database instance: lock}.

Definition at line 250 of file sqlite.py.

◆ path

dblite.engines.sqlite.Database.path

Definition at line 265 of file sqlite.py.

◆ ROW_FACTORY

dblite.engines.sqlite.Database.ROW_FACTORY = None
static

Registered row factory.

Definition at line 253 of file sqlite.py.

Property Documentation

◆ closed

dblite.engines.sqlite.Database.closed = property
static

Whether database is currently not open.

Definition at line 350 of file sqlite.py.

◆ cursor

dblite.engines.sqlite.Database.cursor = property
static

Database engine cursor object, or `None` if closed.

Definition at line 358 of file sqlite.py.

◆ row_factory

dblite.engines.sqlite.Database.row_factory = property
static

The custom row factory, if any, as `function(cursor, row tuple)`.

Definition at line 366 of file sqlite.py.


The documentation for this class was generated from the following file: