db_helpers – Helpers for database managment

Database utilities, mostly SQLite3

class pyhetdex.tools.db_helpers.SQLiteConnector(database, keep_open=':memory:')[source]

Context manager to open and close the database connection.

Returns the current connection when using the instance in a with statement.

Parameters:
database : object

object that represent a database connection and that has the following attributes and methods: database.database, database.connect(), database.close() and either database.connection() or database.get_conn()

keep_open : string, optional

if database.database is equal to keep_open, the database is not opened nor closed. In SQLite3 is useful for in memory databases, that are removed when closed

Examples

>>> import peewee
>>> database = peewee.SqliteDatabase(':memory:')
>>> connect = SQLiteConnector(database)
>>> # the following takes care of opening and closing the connection for
>>> # you, unless the database is on memory
>>> with connect:
...     pass
>>> # this is equivalent, use the form you are more comfortable with
>>> with connect():
...     pass
>>> # the connection is returned (which has also the advantage to connect
>>> # in the case of a in memory database)
>>> with connect() as ctx_conn:
...     pass
pyhetdex.tools.db_helpers.max_sqlite_variables(high=100000)[source]

Get the maximum number of arguments allowed in a query by the current sqlite3 implementation. Based on this question

Parameters:
high : int, optional

maximum number of arguments to test. The number seems a reasonable compromise between execution speed for this function and number of parameters.

Returns:
int

inferred SQLITE_MAX_VARIABLE_NUMBER

pyhetdex.tools.db_helpers._query(cursor, n_args)[source]

Create a query with n_args and execute it