Using the SQLAlchemy package in Python
SQLAlchemy is a top package in Python for interacting with SQL databases. It supports connecting to a wide range of SQL databases, including all the major SQL variants. Here, we will demonstrate connecting to the SQLite database we just created (book_sales.db
). First, let's import SQLAlchemy and connect to the database. If you don't have it installed, use conda (or pip): conda install -c conda-forge sqlalchemy -y
. Let's import SQLAlchemy and connect to the database like so:
from sqlalchemy import create_engine
engine = create_engine('sqlite:///book_sales.db')
connection = engine.connect()
In the preceding example, we first import the create_engine
function from the SQLAlchemy package, then connect to our database file with that function. We are using the relative path to the database file here, but absolute paths are allowed as well. The database type, a colon character, then three forward slashes (///
) is the...