Chapter 14. Databases, Data Science, and a Little AI

Preview

This chapter discusses how to use FastAPI to store and retrieve data. It expands on the simple SQLite examples of Chapter 10 with the following:

  • Other open source databases (relational and not)

  • Higher-level uses of SQLAlchemy

  • Better error checking

Data Storage Alternatives

Note

The term database is unfortunately used to refer to three things:

  • The server type, like PostgreSQL, SQLite, or MySQL

  • A running instance of that server

  • A collection of tables on that server

To avoid confusion—referring to an instance of the last bulleted item above as a “PostgreSQL database database database”—I’ll attach other terms to indicate which one I mean.

The usual backend for a website is a database. Websites and databases are like peanut butter and jelly, and although you could conceivably store your data in other ways (or pair peanut butter with pickles), for this book we’ll stick with databases.

Databases handle many problems that you would otherwise have to solve yourself with code, such as these:

  • Multiple access

  • Indexing

  • Data consistency

The general choices for databases are as follows:

  • Relational databases, with the SQL query language

  • Nonrelational databases, with various query languages

Relational Databases and SQL

Python has a standard relational API definition called DB-API, and it’s supported by Python driver packages for all the major databases. Table 14-1 lists some prominent relational databases ...

Get FastAPI now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.