Skip to main content

SQLite API wrapped into a class with automatic DB schema initialisation and PRAGMA execution upon connection

Project description

SQLite Class wrapper with auto-initialisation of empty DB and PRAGMAs

SQLiteDb class wrapping the operations from the sqlite3 module, providing better support for the with operator and automatic initialisation of an empty DB upon first creation of the DB and setting of the per-connection pragmas.

Additional wrappers are available as a utility, including extraction of a list of tables, extraction of the list of columns of a SELECT query, commit, rollback, start of a transaction, check if the DB is in memory and if it's empty. Executions of SQL script files is also made easy.

Example usage

import sqlitecls

with sqlitecls.SqliteDb('mydata.db', 'mydbschema.sql', 'mypragmas.sql') as db:
    # Now the DB connection is established. If the DB did not exist before,
    # its schema is initialised by running the 'mydbschema.sql' script,
    # preparing it for whatever your application has to do.
    #
    # The 'mypragmas.sql' script is also run EVERY time a connection is
    # opened or re-opened, to ensure each connection to the same file has
    # the correct per-connection PRAGMA settings.
    # For example, imagine we have `PRAGMA synchronous = 3;` in the script
    assert db.pragma('synchronous') == 3

    # You can easily check which tables are available
    tables = db.tables_names()
    # and what columns they have
    columns = db.columns_names('some_table')
    # Otherwise use as any other DB API from now on
    cursor = db.execute('SELECT * FROM mytable')
    # Simplified extraction of the column names from the SELECT query
    columns = sqlitecls.cursor_column_names(cursor)
    # Use as any other DB API from now on
    for row in cursor:
        pass  # Do something with each row
    db.connection, db.cursor  # Accessible for custom operations
    # More utility functions!
    db.start_transaction()
    db.commit()
    db.rollback()
    db.vacuum()
    db.size_bytes()
    db.is_in_memory()
    # Have existing SQL script files? Just run them as they are!
    db.execute_sql_file('myotherfile.sql')  # Load WHOLE file in memory
# Connection automatically closed now

Installation

pip install SQLteCls

or just include the sqlitecls.py file in your project (copy-paste). It's self-contained and has no dependencies other than the standard Python library.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

SQLiteCls-1.0.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

SQLiteCls-1.0.0-py2.py3-none-any.whl (5.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file SQLiteCls-1.0.0.tar.gz.

File metadata

  • Download URL: SQLiteCls-1.0.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for SQLiteCls-1.0.0.tar.gz
Algorithm Hash digest
SHA256 237ba2ede7b956c47b02412c4cac9fc72c24142930bcc9fcc6599ad79d29be9b
MD5 466fe4a907f8744ad05816c626bc6cef
BLAKE2b-256 6e9d5cabe99c28ee58ea4f22b59f27fa3b4d0afe0ca654ead503deaecdd870ff

See more details on using hashes here.

File details

Details for the file SQLiteCls-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: SQLiteCls-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for SQLiteCls-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d02c50dfb4dcb19c7cd90a8af398d45ab0110b93bcf5670d376ba4a46e2cde1c
MD5 0a138fcdbb5ff56496933cce43e5092a
BLAKE2b-256 8532aa63ed266036c10402fed904f7f867cf660f7aa4f7fdbfba8cf57f294395

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page