Aan de slagGa gratis aan de slag

Engines and connection strings

Alright, it's time to create your first engine! An engine is just a common interface to a database, and the information it requires to connect to one is contained in a connection string, for example sqlite:///example.sqlite. Here, sqlite in sqlite:/// is the database driver, while example.sqlite is a SQLite file contained in the local directory.

You can learn a lot more about connection strings in the SQLAlchemy documentation.

Your job in this exercise is to create an engine that connects to a local SQLite file named census.sqlite. Then, print the names of the tables the engine contains using the .table_names() method. Note that when you just want to print the table names, you do not need to use engine.connect() after creating the engine.

Deze oefening maakt deel uit van de cursus

Introduction to Databases in Python

Cursus bekijken

Oefeninstructies

  • Import create_engine from the sqlalchemy module.
  • Using the create_engine() function, create an engine for a local file named census.sqlite with sqlite as the driver. Be sure to enclose the connection string within quotation marks.
  • Print the output from the .table_names() method on the engine.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Import create_engine


# Create an engine that connects to the census.sqlite file: engine
engine = ____

# Print table names
print(____)
Code bewerken en uitvoeren