Querying SQLite from Python

In our Querying SQLite from Python mission, you’ll learn how to query a SQLite database using the sqlite3 library. 

Being able to write SQL queries using Python can be quite beneficial. Otherwise, you would find yourself writing code in Python, exporting data from Python, importing that in a separate program you have that can query data, saving that data, and importing it back into Python. That would be, needless to say, a cumbersome process! 

In this mission, you will learn how to avoid that process by writing queries for a SQLite database from within Python using the sqlite3 library. As you progress through the mission, you will get familiar with connecting to SQLite databases and build experience with the SQL query workflow.

Throughout this mission, you’ll also continue your analysis of the 2010-2012 college grade data to give you practice using SQL to solve real-world data science problems. 


  • How to run SQL queries using sqlite3 in Python.
  • How to work with cursors and tuples.

Mission Outline

1. Overview
2. Introduction to the Data
3. Connecting to the Database
4. Introduction to Cursor Objects and Tuples
5. Working With Sequences of Values as Tuples
6. Creating a Cursor and Running a Query
7. Execute as a Shortcut for Running a Query
8. Fetching a Specific Number of Results
9. Closing the Database Connection
10. Practice
11. Next Steps
12. Takeaways


Course Info:


The median completion time for this course is 6.3 hours. View Details

This course includes five missions and one guided project.  It is the 11th course in the Data Analyst in Python path and the Data Scientist in Python path.


Take a Look Inside

(function(d) { d.addEventListener("DOMContentLoaded", function() { var pathname = d.location.pathname.replace(/^[/]|[/]$/g, "").replace("/", "-"); var tags = d.getElementsByTagName("iframe"); var type = pathname.startsWith("course") ? "?course=" : pathname.startsWith("path") ? "?path=" : null; if (type) { var i; for (i = 0; i < tags.length; i++) { if (tags[i].src.indexOf("signup#iframe") !== -1) { tags[i].src = tags[i].src.replace("#iframe", "") + type + pathname + "#iframe"; } } } }, false); })(document);