Getting Started with SQLite Databases
Learn the basics of SQLite databases and how to interact with them using Python.
Introduction to SQLite
SQLite is a self-contained, file-based database system that allows you to store and manage data in a structured way. It's a great choice for small to medium-sized projects, and is widely used in many applications.
Installing SQLite
To get started with SQLite, you'll need to install it on your system. You can download the latest version from the official SQLite website.
Interacting with SQLite using Python
To interact with SQLite databases using Python, you'll need to install the sqlite3 module. You can do this by running pip install pysqlite3 in your terminal.
Connecting to a Database
To connect to a SQLite database, you can use the following code:
import sqlite3
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
Creating a Table
To create a table in your database, you can use the following code:
cursor.execute '''
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL
)
'''
Inserting Data
To insert data into your table, you can use the following code:
cursor.execute '''
INSERT INTO users (name, email)
VALUES ('John Doe', 'john@example.com')
'''
conn.commit()
Retrieving Data
To retrieve data from your table, you can use the following code:
cursor.execute '''
SELECT * FROM users
'''
rows = cursor.fetchall()
for row in rows:
print(row)
Conclusion
In this tutorial, you've learned the basics of SQLite databases and how to interact with them using Python. You've also learned how to create a table, insert data, and retrieve data.
Ready for more? These paid resources pick up where this lesson leaves off.
Project-based sqlite video courses — the perfect paid next step after these free lessons.
Browse on Udemy →Hand-picked sqlite books to master the fundamentals offline.
See on Amazon →Some links on this page are affiliate links: we may earn a commission at no extra cost to you. We only recommend tools we believe are genuinely useful.