How to retrieve data entries from SQLite3 using Python

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

SQLite is one of the fastest ways to set up a SQL-ready database.

Once you store data in an SQLite3 you can easily reuse it where you want.

Here is how to retrieve data entries from an SQLite3 Database.

Here is the code

First, we connect to the database

import sqlite3

# We create the connection
con = sqlite3.connect('example.db')

# We create a cursor to perform our requests
cur = con.cursor()
We connect to the database or create if doesn't exist

Second, we create our table and fill it up with dummy data

# Create a table
cur.execute("CREATE TABLE revenues (date text, amount float, source text)")

# Insert multiple entries in the database
entries = [
    ('2020-11-31', 12349.2, 'shopify'),
    ('2020-12-31', 14523.2, 'shopify'),
    ('2021-01-31', 18029.4, 'shopify'),
    ('2021-02-29', 51023.3, 'shopify'),
    ('2021-03-31', 94400.3, 'shopify'),
]
cur.executemany("INSERT INTO revenues VALUES (?,?,?)", entries)

# Save (commit) the changes
con.commit()
We create the connection, insert the data and commit the changes

Third, we retrieve data

One entry

cur.execute("select * from revenues").fetchone()

All entries

cur.execute("select * from revenues").fetchall()

All entries that match the condition

cur.execute("select * from revenues where amount >= 14000").fetchall()

Here you are! You now know how to retrieve data from an SQLite3 Database.

You might be interested in

How to install SQLite3 in Python
Learn how to install SQLite 3 in Python
How to create an SQLite3 Database using Python
Learn how to create an SQLite3 database using Python.
How to connect to an SQLite3 database using Python
Learn how to connect to an SQLite3 database using python.
How to add a new table in SQLite3 using Python
Learn how to add a new table in SQLite3 using Python.
How to add an entry to an SQLite database
Learn how to add an entry to an SQLite database using the execute() method.
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.