Handling a ResultSet
Recall the differences between a ResultProxy and a ResultSet:
- ResultProxy: The object returned by the
.execute()
method. It can be used in a variety of ways to get the data returned by the query. - ResultSet: The actual data asked for in the query when using a fetch method such as
.fetchall()
on a ResultProxy.
This separation between the ResultSet and ResultProxy allows us to fetch as much or as little data as we desire.
Once we have a ResultSet, we can use Python to access all the data within
it by column name and by list style indexes. For example, you can get the first
row of the results by using results[0]
. With that first row then assigned to a variable first_row
, you can get
data from the first column by either using first_row[0]
or by column name such
as first_row['column_name']
. You'll now practice exactly this using the ResultSet you obtained from the census
table in the previous exercise. It is stored in the variable results
. Enjoy!
This is a part of the course
“Introduction to Databases in Python”
Exercise instructions
- Extract the first row of
results
and assign it to the variablefirst_row
. - Print the value of the first column in
first_row
. - Print the value of the
'state'
column infirst_row
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Get the first row of the results by using an index: first_row
first_row = ____
# Print the first row of the results
print(first_row)
# Print the first column of the first row by accessing it by its index
print(____)
# Print the 'state' column of the first row by using its name
print(____)
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.
Chapter 1: Basics of Relational Databases
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!Chapter 2: Applying Filtering, Ordering and Grouping to Queries
In this chapter, you will build on your database knowledge by writing more nuanced queries that allow you to filter, order, and count your data—all within the Pythonic framework provided by SQLAlchemy.
Exercise 1: Filtering and targeting dataExercise 2: Connecting to a PostgreSQL databaseExercise 3: Filter data selected from a Table - SimpleExercise 4: Filter data selected from a Table - ExpressionsExercise 5: Filter data selected from a Table - AdvancedExercise 6: Ordering query resultsExercise 7: Ordering by a single columnExercise 8: Ordering in descending order by a single columnExercise 9: Ordering by multiple columnsExercise 10: Counting, summing, and grouping dataExercise 11: Counting distinct dataExercise 12: Count of records by stateExercise 13: Determining the population sum by stateExercise 14: SQLAlchemy and pandas for visualizationExercise 15: ResultsSets and pandas DataFramesExercise 16: From SQLAlchemy results to a plotChapter 3: Advanced SQLAlchemy Queries
In this chapter, you will learn to perform advanced—and incredibly useful—queries that enable you to interact with your data in powerful ways.
Exercise 1: Calculating values in a queryExercise 2: Connecting to a MySQL databaseExercise 3: Calculating a difference between two columnsExercise 4: Determining the overall percentage of womenExercise 5: SQL relationshipsExercise 6: Automatic joins with an established relationshipExercise 7: JoinsExercise 8: More practice with joinsExercise 9: Working with hierarchical tablesExercise 10: Using alias to handle same table joined queriesExercise 11: Leveraging functions and group_bys with hierarchical dataExercise 12: Handling large ResultSetsExercise 13: Working on blocks of recordsChapter 4: Creating and Manipulating your own Databases
In the previous chapters, you interacted with existing databases and queried them in different ways. Now, you will learn how to build your own databases and keep them updated.
Exercise 1: Creating databases and tablesExercise 2: Creating tables with SQLAlchemyExercise 3: Constraints and data defaultsExercise 4: Inserting data into a tableExercise 5: Inserting a single rowExercise 6: Inserting multiple records at onceExercise 7: Loading a CSV into a tableExercise 8: Updating data in a tableExercise 9: Updating individual recordsExercise 10: Updating multiple recordsExercise 11: Correlated updatesExercise 12: Deleting data from a databaseExercise 13: Deleting all the records from a tableExercise 14: Deleting specific recordsExercise 15: Deleting a table completelyChapter 5: Putting it all together
Bring together all of the skills you acquired in the previous chapters to work on a real-life project. From connecting to a database and populating it, to reading and querying it.
Exercise 1: Census case studyExercise 2: Setup the engine and metadataExercise 3: Create the table to the databaseExercise 4: Populating the databaseExercise 5: Reading the data from the CSVExercise 6: Load data from a list into the TableExercise 7: Querying the databaseExercise 8: Determine the average age by populationExercise 9: Determine the percentage of population by gender and stateExercise 10: Determine the difference by state from the 2000 and 2008 censusesExercise 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.