Create A Database

For this tutorial, we'll use the SQLAlchemy Python library to communicate with a database.

Let's import it:

In [1]:
import sqlalchemy as sa

SQLAlchemy uses an "engine" object to interact with a database.

To create an engine, we first need to choose a Database Management System (DBMS). We'll use SQLite as our DBMS.

Next, we have to define a connection URL. The URL tells SQLAlchemy which DBMS we have chosen and how to connect to our database.

SQLite uses a single file for each database and the structure of the URL we need for SQLAlchemy is:

sqlite:///<path to the database file>

Our example database will contain data about High Altitude Balloon flights. Let's name our database file flight.db and create an engine object for it:

In [2]:
engine = sa.create_engine('sqlite:///flight.db')

We can now connect to our database using the connect() method of our engine.

SQLAlchemy will look for the file we specified in our URL and establish a connection to our database. If the file doesn't exist, sqlalchemy will create it and then make the connection.

In [3]:
connection = engine.connect()

You should now be able to see a new 'flight.db' file in the same directory as this notebook.

If you opted to run the code on your own computer and also installed the graphical tool, you can also now start that program and open your new 'flight.db' file.