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
Python has a built-in module called sqlite3 that allows you to interact with SQLite databases. Here's an example of how to create a new database and insert some data:
import sqlite3
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
cursor.execute('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)')
cursor.execute('INSERT INTO users (name, email) VALUES (?, ?)', ('John Doe', 'john@example.com'))
conn.commit()
conn.close()
Retrieving Data from SQLite
To retrieve data from your SQLite database, you can use the SELECT statement. Here's an example:
import sqlite3
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM users')
rows = cursor.fetchall()
for row in rows:
print(row)
conn.close()
Updating and Deleting Data
You can update data in your SQLite database using the UPDATE statement, and delete data using the DELETE statement. Here are some examples:
import sqlite3
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
cursor.execute('UPDATE users SET name = ? WHERE id = ?', ('Jane Doe', 1))
cursor.execute('DELETE FROM users WHERE id = ?', (1,))
conn.commit()
conn.close()
``
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.