liveBook · Manning

Chapter 23: Saving data

What you will learn in chapter 23 of The Quick Python Book, Fourth Edition

The chapter introduction explains why cleaned data may need a database and identifies relational and NoSQL databases as useful storage options.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction explains why cleaned data may need a database and identifies relational and NoSQL databases as useful storage options.

Key terms: database, store, retrieve, relational databases, NoSQL databases, PostgreSQL, MySQL, SQL Server, MongoDB

23.1 Relational databases

This section positions relational databases as an established way to store data and explains why sqlite3 can serve as an introduction to Python database use.

Key terms: relational databases, sqlite3

23.1.1 The Python database API

This subsection explains why Python code for accessing different SQL databases can look similar.

Key terms: PEP-249, Database API, DB-API, SQLite, PostgreSQL, MySQL

23.2 SQLite: Using the sqlite3 database

This section explains when SQLite is useful and how to connect to a database, run parameterized SQL, read results, and save changes with Python’s sqlite3 module.

Key terms: sqlite3, standard library, local file, connect, Connection object, Cursor object, :memory:, ?, execute, variable binding

23.3 Using MySQL, PostgreSQL, and other relational databases

This section explains why Python code for other relational databases resembles SQLite code but is not always portable.

Key terms: DB-API, database server, connection, parameters, queries

23.4 Making database handling easier with an object relational mapper

The section explains why raw SQL through DB-API can be cumbersome and introduces object relational mappers as a Python-oriented alternative.

Key terms: DB-API, raw SQL, SQL variant, object relational mapper, ORM, relational database, Python objects

23.4.1 SQLAlchemy

This subsection shows how to set up SQLAlchemy, work with a relational table through its API or a mapped class, and why an ORM can be useful.

Key terms: SQLAlchemy, ORM, relational database, SQL, create_engine, MetaData, Table, Column, sessionmaker, Session

23.4.2 Using Alembic for database schema changes

This section explains why database migrations are useful and shows how Alembic tracks, applies, and reverses a SQLAlchemy database schema change.

Key terms: database migration tool, schema, migrations, Alembic, alembic.ini, revision script, down_revision, upgrade(), downgrade(), upgrade

23.5 NoSQL databases

This section introduces NoSQL databases as alternatives to relational tables and explains their varied data models and potential retrieval advantages.

Key terms: NoSQL databases, key-value pairs, indexed documents, graphs, normalized, retrieving information

23.6 Key-value stores with Redis

Introduces Redis as a fast, networked key-value store and demonstrates basic Python operations, expiring cache entries, and persistence considerations.

Key terms: Redis, key-value store, in-memory, caching, message broker, redis-py, Redis connection, keys(), incr, rpush

23.7 Documents in MongoDB

This section introduces MongoDB’s document model and shows how Python uses pymongo to connect to a server and work with documents in collections.

Key terms: MongoDB, document-based database, BSON, documents, pymongo, MongoClient, database, collection, document, insert()

23.8 Creating a database

This section sets an open-ended challenge to choose a database for a previously discussed dataset, load the data, and retrieve matching records.

Key terms: dataset, type of database, search criteria, single and multiple matching records

23.9 Summary

Concepts explained in this chapter

← Chapter 22: Data over the network Chapter 24: Exploring data →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.