{"id":2388,"library":"apsw","title":"Another Python SQLite Wrapper","description":"APSW (Another Python SQLite Wrapper) is a comprehensive Python wrapper for the SQLite embedded relational database engine. It provides a thin, complete layer over the SQLite C API, staying up-to-date with both SQLite and Python. APSW offers extended functionality beyond the standard `sqlite3` module, including full text search, session, virtual tables, VFS, and advanced JSON support. It supports CPython 3.10 onwards and releases are approximately quarterly, mirroring SQLite's release cycle.","status":"active","version":"3.53.0.0","language":"en","source_language":"en","source_url":"https://github.com/rogerbinns/apsw","tags":["database","sqlite","wrapper","async","orm"],"install":[{"cmd":"pip install apsw","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required runtime environment.","package":"Python","version":">=3.10"}],"imports":[{"note":"The primary classes like Connection and Cursor are direct attributes of the top-level 'apsw' module, not typically imported directly.","wrong":"from apsw import Connection","symbol":"Connection","correct":"import apsw\nconnection = apsw.Connection('database.db')"},{"note":"Similar to Connection, Cursor objects are created via a Connection instance.","wrong":"from apsw import Cursor","symbol":"Cursor","correct":"import apsw\ncursor = connection.cursor()"}],"quickstart":{"code":"import apsw\nimport os\n\ndb_file = 'example.db'\nif os.path.exists(db_file):\n    os.remove(db_file)\n\n# Best practice application for optimal settings and error avoidance\napsw.bestpractice.apply(apsw.bestpractice.recommended)\n\n# Use a context manager to ensure proper closing and transaction management\nwith apsw.Connection(db_file) as connection:\n    cursor = connection.cursor()\n\n    # Create a table\n    cursor.execute(\"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)\")\n\n    # Insert data\n    cursor.execute(\"INSERT INTO users (name, email) VALUES (?, ?)\", ('Alice', 'alice@example.com'))\n    cursor.execute(\"INSERT INTO users (name, email) VALUES (?, ?)\", ('Bob', 'bob@example.com'))\n\n    # Select data\n    print(\"All users:\")\n    for row in cursor.execute(\"SELECT id, name, email FROM users\"):\n        print(f\"ID: {row[0]}, Name: {row[1]}, Email: {row[2]}\")\n\n    # Select with named bindings\n    print(\"\\nUser with ID 1:\")\n    for row in cursor.execute(\"SELECT name FROM users WHERE id = :id\", {'id': 1}):\n        print(f\"Name: {row[0]}\")\n\nprint(f\"Database '{db_file}' operations complete.\")\n","lang":"python","description":"This example demonstrates how to establish a connection, apply best practices, create a table, insert data using positional and named bindings, and retrieve data. It uses a context manager for connection handling, which is the recommended approach."},"warnings":[{"fix":"Avoid using APSW 3.52.0.0. Be aware of the feature exclusions if migrating from a hypothetical 3.52.0.0 to 3.51.3.0, or generally ensure your SQLite features are covered by the current APSW version.","message":"SQLite 3.52.0 withdrawal led to the withdrawal of APSW 3.52.0.0. While APSW 3.51.3.0 includes most of its features, it excludes `SQLITE_UTF8_ZT` and `sqlite3_carray_bind_v2`.","severity":"breaking","affected_versions":"3.52.0.0 (withdrawn), 3.51.3.0"},{"fix":"For concurrent database access, use a separate `apsw.Connection` object for each thread. APSW *does* release the GIL around SQLite calls, allowing Python threads to execute concurrently with different connections.","message":"APSW `Connection` and `Cursor` objects are not thread-safe for concurrent use. Attempting to use the same `Connection` or `Cursor` object simultaneously in multiple threads will raise a `ThreadingViolationError` or lead to hangs.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always use `with apsw.Connection(...) as connection:` and manage cursors within that context. Explicitly call `.close()` on objects if context managers are not feasible.","message":"While APSW objects automatically close on garbage collection, it is best practice to explicitly close `Connection` and `Cursor` objects or use context managers (`with apsw.Connection(...)`) to ensure timely resource release and proper transaction handling, especially if user-defined functions or collations introduce circular references preventing automatic garbage collection.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Call `apsw.bestpractice.apply(apsw.bestpractice.recommended)` at the start of your application or when opening a connection to enable these best practices.","message":"The `apsw.bestpractice` module offers functions to enable recommended SQLite settings (e.g., WAL mode, foreign keys, busy timeout, query optimization) which are not default due to SQLite's strong backward compatibility. Not applying these can lead to common mistakes or suboptimal performance.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your Linux deployment environment meets the minimum glibc version requirements introduced by `cibuildwheel` v3. Check `cibuildwheel` documentation for specifics if encountering compatibility issues.","message":"Starting with APSW 3.50.2.0, the PyPI binary builds began using `cibuildwheel` version 3, which advanced the minimum supported Linux distribution. This might affect deployments to older Linux environments.","severity":"gotcha","affected_versions":">=3.50.2.0"},{"fix":"Keep your Python runtime updated to actively supported versions (currently >=3.10). Consult the APSW changelog for specific Python EOL-related support drops.","message":"APSW follows Python's end-of-life (EOL) cycle for supported versions. Once a Python release goes EOL, there will be one final APSW release supporting that Python version, after which support is dropped.","severity":"deprecated","affected_versions":"E.g., Python 3.8 support removed in 3.47.0.0, Python 3.9 support removed in 3.50.0.0 or 3.50.4.0."}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}