SQLBag Utilities
SQLBag (version 0.1.1617247075) provides various snippets of SQL-related boilerplate, simplifying common SQLAlchemy tasks such as session management, query execution (e.g., `one`, `many`, `chunks`), engine creation, and upsert operations. It aims to reduce repetitive code for database interactions. Releases are frequent, following a timestamp-based micro-versioning system, primarily reflecting a single developer's utility collection.
Common errors
-
ModuleNotFoundError: No module named 'sqlalchemy'
cause sqlbag relies heavily on SQLAlchemy but does not declare it as a direct dependency in `install_requires`.fixInstall SQLAlchemy explicitly: `pip install sqlalchemy`. -
AttributeError: 'Session' object has no attribute 'one'
cause You are attempting to call a `sqlbag` helper function (like `one`, `many`, `chunks`) directly on an SQLAlchemy `Session` object. These are global `sqlbag` functions that take a session as their first argument.fixCall the function from the `sqlbag` module, passing the session: `sqlbag.one(session, query)` instead of `session.one(query)`. -
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: users
cause The database table you are trying to query or interact with does not exist in the specified database.fixEnsure your table schema is created before attempting to interact with it. For example, by executing `CREATE TABLE` statements or using SQLAlchemy's ORM metadata to create tables.
Warnings
- gotcha SQLBag implicitly requires SQLAlchemy but does not list it in its `install_requires`. Users must manually install `SQLAlchemy` for `sqlbag` to function.
- gotcha SQLBag uses a timestamp-based micro-versioning scheme (e.g., `0.1.1617247075`). This indicates that API stability is not guaranteed, and breaking changes might occur frequently without explicit major version bumps or clear announcements in the release notes.
- gotcha The documentation for `sqlbag` is minimal, primarily consisting of the GitHub README. Advanced usage or debugging often requires inspecting the source code.
Install
-
pip install sqlbag
Imports
- S
from sqlbag import S
- one
from sqlbag import one
- many
from sqlbag import many
- get_engine
from sqlbag import get_engine
from sqlbag.engine import get_engine
- make_session
from sqlbag import make_session
from sqlbag.engine import make_session
- upsert
from sqlbag import upsert
from sqlbag.sqla import upsert
Quickstart
import os
from sqlbag import S, one, many
from sqlbag.engine import get_engine
# Configure your database URL
database_url = os.environ.get('DATABASE_URL', 'sqlite:///./test.db')
engine = get_engine(url=database_url)
# Example usage with S context manager
with S(engine) as session:
session.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)')
session.execute("INSERT INTO users (name) VALUES ('Alice')")
session.execute("INSERT INTO users (name) VALUES ('Bob')")
session.commit()
# Fetch a single result
alice = one(session, 'SELECT * FROM users WHERE name = :name', name='Alice')
print(f"Found one: {alice}")
# Fetch multiple results
all_users = many(session, 'SELECT * FROM users')
print(f"Found many: {all_users}")
print("SQLBag quickstart completed successfully.")