Viewing Table details
Great job reflecting the census
table! Now you can begin to learn more about the columns and
structure of your table. It is important to get an understanding of your database by examining the column names. This can be done by
using the .columns
attribute and accessing the .keys()
method. For example, census.columns.keys()
would return a list of
column names of the census
table.
Following this, we can use the metadata container to find out more details about the reflected table such as the columns and their types.
For example, information about the table objects are stored in the metadata.tables
dictionary, so you can get the metadata of your census
table with metadata.tables['census']
. This is similar to your use of the repr()
function on the census
table from the previous exercise.
The code for connecting to the engine and initializing the metadata you wrote in the previous exercises is displayed for you again and for the last time. From now on and until Chapter 5, this will usually be done behind the scenes.
This is a part of the course
“Introduction to Databases in Python”
Exercise instructions
- Reflect the
census
table as you did in the previous exercise using theTable()
function. - Print a list of column names of the
census
table by applying the.keys()
method tocensus.columns
. - Print the details of the
census
table using themetadata.tables
dictionary along with therepr()
function. To do this, first access the'census'
key of themetadata.tables
dictionary, and place this inside the providedrepr()
function.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
from sqlalchemy import create_engine, MetaData, Table
engine = create_engine('sqlite:///census.sqlite')
metadata = MetaData()
# Reflect the census table from the engine: census
census = ____(____, ____, autoload=____, autoload_with=____)
# Print the column names
print(____)
# Print full metadata of census
print(repr(____))
This exercise is part of the course
Introduction to Databases in Python
In this course, you'll learn the basics of relational databases and how to interact with them.
In this chapter, you’ll get acquainted with the fundamentals of relational databases and the relational model for database management. You will learn how to connect to a database and interact with it by writing basic SQL queries, both in raw SQL as well as SQLAlchemy, which provides a Pythonic way of interacting with databases.
Exercise 1: Introduction to DatabasesExercise 2: Relational modelExercise 3: Connecting to your databaseExercise 4: Engines and connection stringsExercise 5: Autoloading Tables from a databaseExercise 6: Viewing Table detailsExercise 7: Introduction to SQL queriesExercise 8: Selecting data from a Table: raw SQLExercise 9: Selecting data from a Table with SQLAlchemyExercise 10: Handling a ResultSetExercise 11: Congratulations!What is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.