Last modified: Sep 22, 2026
How to Import SQLite in Python
Python provides a built-in module called sqlite3 to work with SQLite databases. This module is part of Python’s standard library since version 2.5. You do not need to install anything extra to use it. Simply importing the module allows you to create, manage, and interact with SQLite databases directly in your Python scripts.
Why Use SQLite with Python?
SQLite is a lightweight, file-based database engine. It requires no separate server process. Python developers often prefer SQLite for prototyping, testing, and small applications due to its simplicity and ease of use.
Basic Syntax to Import SQLite in Python
The first step to using SQLite in Python is importing the sqlite3 module. This is done using the standard import statement:
# Importing the sqlite3 module
import sqlite3
print("SQLite imported successfully!")
Output:
SQLite imported successfully!
Connecting to a Database
Once imported, you can connect to an existing SQLite database or create a new one using the connect() function. If the specified database file does not exist, Python will automatically create it.
import sqlite3
# Connecting to a database (or creating one)
conn = sqlite3.connect('example.db')
print("Database connected successfully.")
Output:
Database connected successfully.
Creating a Cursor Object
After establishing a connection, you need a cursor() object to execute SQL commands. This object acts as a pointer to the database.
# Creating a cursor object
cursor = conn.cursor()
print("Cursor created.")
Output:
Cursor created.
Executing SQL Commands
Use the execute() method on the cursor object to run SQL queries. For example, creating a table:
# Creating a table
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER
)
''')
print("Table created successfully.")
Output:
Table created successfully.
Inserting Data into the Table
You can insert data using the execute() method with parameterized queries to prevent SQL injection.
# Inserting data
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ('Alice', 30))
conn.commit() # Save changes
print("Data inserted successfully.")
Output:
Data inserted successfully.
Querying Data from the Table
To retrieve data, use the execute() method again. Then call fetchone() or fetchall() to get results.
# Querying data
cursor.execute("SELECT * FROM users")
rows = cursor.fetchall()
for row in rows:
print(row)
Output:
(1, 'Alice', 30)
Updating Records
Use the execute() method to update existing records:
# Updating data
cursor.execute("UPDATE users SET age = ? WHERE name = ?", (31, 'Alice'))
conn.commit()
print("Record updated successfully.")
Output:
Record updated successfully.
Deleting Records
Delete records using the execute() method:
# Deleting data
cursor.execute("DELETE FROM users WHERE name = ?", ('Alice',))
conn.commit()
print("Record deleted successfully.")
Output:
Record deleted successfully.
Closing the Connection
Always close the database connection after finishing your operations using the close() method.
# Closing the connection
conn.close()
print("Connection closed.")
Output:
Connection closed.
Best Practices When Using SQLite in Python
- Use parameterized queries to avoid SQL injection.
- Always commit transactions when modifying data.
- Close connections properly to avoid resource leaks.
- Handle exceptions using try-except blocks for robustness.
Conclusion
Importing and using SQLite in Python is straightforward with the built-in sqlite3 module. By following the steps outlined above, beginners can quickly start building database-driven applications. Whether you're storing user data, logging events, or managing configuration settings, SQLite offers a reliable and efficient solution. With proper handling of connections, cursors, and queries, you can build scalable and maintainable Python applications backed by SQLite databases.
For more advanced usage, check out our guide on Python SQLite API Guide: Build Database Apps.