Python > Working with Data > Databases > SQLite - using the `sqlite3` module
Creating and Querying a SQLite Database
This snippet demonstrates how to create a SQLite database, insert data into a table, and query the data using the `sqlite3` module in Python. This is a fundamental example for interacting with SQLite databases.
Connecting to the Database and Creating a Table
This part connects to the 'mydatabase.db' SQLite database. If the database doesn't exist, it will be created. A cursor object is created, which allows you to execute SQL queries. The `CREATE TABLE` command defines the structure of the 'users' table with columns for 'id', 'name', and 'email'. The `IF NOT EXISTS` clause ensures that the table is only created if it doesn't already exist. Finally, `conn.commit()` saves the changes to the database.
import sqlite3
# Connect to the SQLite database (or create it if it doesn't exist)
conn = sqlite3.connect('mydatabase.db')
# Create a cursor object to execute SQL commands
cursor = conn.cursor()
# Create a table named 'users' if it doesn't exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE
)
''')
# Commit the changes to the database
conn.commit()
Inserting Data into the Table
This part inserts two rows of data into the 'users' table. Each `cursor.execute()` call inserts a new user with their name and email. The `conn.commit()` call saves these changes to the database file.
# Insert data into the 'users' table
cursor.execute("INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com')")
cursor.execute("INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com')")
# Commit the changes
conn.commit()
Querying Data from the Table
This part queries all rows from the 'users' table using `SELECT * FROM users`. The `cursor.fetchall()` method retrieves all the results as a list of tuples. The code then iterates through the rows and prints each row. Finally, `conn.close()` closes the connection to the database, releasing resources.
# Query all data from the 'users' table
cursor.execute("SELECT * FROM users")
# Fetch all the results
rows = cursor.fetchall()
# Print the results
for row in rows:
print(row)
# Close the connection
conn.close()
Concepts behind the snippet
This snippet showcases the core concepts of SQLite database interaction: connecting to a database, creating tables, inserting data, querying data, and closing the connection. It's a basic CRUD (Create, Read, Update, Delete) operation example.
Real-Life Use Case Section
This snippet is valuable in applications where you need to store structured data locally, such as user settings, application data, or cached information. For example, a desktop application might use SQLite to store user preferences, or a mobile app might use it to cache data retrieved from a remote server. This avoids the need for external database servers in simpler applications.
Best Practices
Always close the database connection after you're finished using it to release resources. Use parameterized queries (see the next snippet) to prevent SQL injection vulnerabilities. Consider using a context manager (`with sqlite3.connect(...) as conn:`) to ensure the connection is closed automatically, even if errors occur.
Interview Tip
Be prepared to explain the purpose of each step in the code, especially the `CREATE TABLE` statement and the `cursor.execute()` calls. Understand the difference between `fetchone()`, `fetchall()`, and `fetchmany()` methods for retrieving data. Be ready to discuss potential vulnerabilities like SQL injection and how to prevent them.
When to use them
SQLite is ideal for small to medium-sized applications that require local data storage without the overhead of a full-fledged database server. It's a good choice when you need a simple, file-based database for single-user applications or embedded systems.
Memory footprint
SQLite has a relatively small memory footprint compared to server-based databases like MySQL or PostgreSQL. This makes it suitable for resource-constrained environments. The actual memory usage depends on the size of the database and the complexity of the queries.
alternatives
Alternatives to SQLite include server-based databases like MySQL, PostgreSQL, MongoDB, and cloud-based databases like Amazon RDS or Google Cloud SQL. The choice depends on factors such as scalability requirements, data complexity, and the number of concurrent users.
pros
SQLite's advantages include its simplicity, zero-configuration setup, portability (it's a single file), and small footprint. It's embedded directly into the application, so there's no need for a separate database server. It also supports standard SQL syntax.
cons
SQLite's disadvantages include its limited concurrency (it's not designed for high-volume multi-user access), lack of support for some advanced SQL features, and potential performance limitations for very large datasets or complex queries. It's not suitable for applications requiring high availability or distributed data management.
FAQ
-
What is the purpose of the cursor object?
The cursor object allows you to execute SQL commands and retrieve results from the database. It acts as an intermediary between your Python code and the SQLite database engine. -
What is the difference between `commit()` and `close()`?
`commit()` saves the changes you've made to the database, while `close()` closes the connection to the database, releasing resources. You must `commit()` your changes for them to be persistent.