liveBook · Manning

SQLite

As explained in Practices of the Python Pro

SQLite is a lightweight relational database engine that stores an entire database in a single file.

SQLite is a portable database that stores data in a single file. A Python sqlite3 connection can create a missing database file, making it easy to start over during development. section 6.3.1

Within a persistence layer, SQLite receives application data through SQL statements. A DatabaseManager can isolate connections and database operations from the rest of the application. section 6.3.1

A SQLite database can support the main CRUD tasks:

  • Create tables and initialize the database. section 6.3.1
  • Add, delete, list, select, sort, and count records. section 6.3.1
  • Use placeholders and values when inserting or filtering records. section 6.3.1

Bookmarks database

A bookmarks table can store an automatically generated ID, required title and URL fields, optional notes, and a required date_added value. Records can then be inserted, selected by ID, sorted by title, or deleted by criteria. section 6.3.1

import sqlite3


class DatabaseManager:
    def __init__(self, database_filename):
        self.connection = sqlite3.connect(database_filename)  #1

    def __del__(self):
        self.connection.close()                               #2

Connect to a database file when creating DatabaseManager, then close the connection when the manager is destroyed. section 6.3.1

How Practices of the Python Pro covers it

How deep it goes
Explains single-file storage, connections, SQL statements, and CRUD methods, but stops before advanced database packages.
Examples
Uses a bookmarks table, parameterized inserts, record deletion, selection, filtering, and title sorting.
What it assumes
Assumes basic Python and application-layer separation while introducing database connections, SQL statements, and persistence responsibilities.
What you can do after
Operate/configure a SQLite database through Python connections, tables, parameterized queries, and CRUD methods.
How it connects
Connects SQLite with persistence layers, SQL, CRUD operations, Python, and database managers.

Also explained in

Compare how 15 books explain SQLite →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.