This chapter introduces SQLAlchemy’s
MetaData classes. The
class provides database connectivity, including a connection pool with various strategies for acquiring connections from the
MetaData class maintains information about
your database schema, including any tables and indexes defined. In this
chapter, you will learn how to define a new database schema using
MetaData as well as how to connect a
MetaData instance to an existing schema.
Engine class is responsible for
managing the connection to the database. It does this by incorporating a
database connection pool and a database-specific
to translate the SQL expression language (Chapter 5)
into database-specific SQL.
To get started using an
# Create a connection to a SQLite in-memory database engine = create_engine('sqlite://') # Create a connection to a SQLite on-disk database "data.sqlite" engine = create_engine('sqlite:///data.sqlite') # Create a connection to a PostGreSQL database engine = create_engine('postgres://rick:foo@localhost:5432/pg_db') # Create a connection to a MySQL database engine = create_engine('mysql://localhost/mysql_db') # Create a connection to an Oracle database (via TNS) engine = create_engine('oracle://rick:foo@oracle_tns') # Create a connection to an Oracle database (without a TNS name) engine = ... create_engine('oracle://rick:foo@localhost:1521/oracle_sid') ...