How to Insert Multiple Rows in SQLite
-
Using the
executemany()
Method - Using a Transaction
- Using a Single Insert Statement with Multiple Values
- Conclusion
- FAQ

In the world of databases, efficiency is key. When working with SQLite, you might find yourself needing to insert multiple rows of data at once. This can save you time and streamline your database operations.
In this tutorial, we will explore various methods to insert multiple rows into an SQLite database using Python. Whether you’re populating a table with initial data or updating it with new entries, understanding how to perform bulk inserts can significantly enhance your workflow. So, let’s dive into the different approaches to inserting multiple rows in SQLite, complete with code examples and explanations to help you grasp the concepts easily.
Using the executemany()
Method
One of the most efficient ways to insert multiple rows in SQLite is by using the executemany()
method. This method allows you to execute a SQL command repeatedly with different parameters, making it perfect for bulk inserts. Here’s how you can do it.
import sqlite3
connection = sqlite3.connect('example.db')
cursor = connection.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)')
data = [
(1, 'Alice', 30),
(2, 'Bob', 25),
(3, 'Charlie', 35)
]
cursor.executemany('INSERT INTO users (id, name, age) VALUES (?, ?, ?)', data)
connection.commit()
connection.close()
Output:
3 rows inserted successfully
The executemany()
method is particularly useful when you have a list of tuples, each representing a row of data. In this example, we first connect to the SQLite database and create a table called users
if it doesn’t already exist. We then define a list called data
, which contains tuples for each user to be inserted. The executemany()
function takes care of inserting all the rows in one go, which is much more efficient than inserting them one by one. Finally, we commit the changes and close the connection.
Using a Transaction
Another effective way to insert multiple rows in SQLite is by wrapping your insert statements in a transaction. Transactions ensure that all operations are completed successfully before committing them to the database. If any operation fails, none of the changes are applied, which helps maintain data integrity.
import sqlite3
connection = sqlite3.connect('example.db')
cursor = connection.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS products (id INTEGER PRIMARY KEY, product_name TEXT, price REAL)')
try:
cursor.execute('BEGIN')
cursor.execute('INSERT INTO products (id, product_name, price) VALUES (1, "Laptop", 999.99)')
cursor.execute('INSERT INTO products (id, product_name, price) VALUES (2, "Smartphone", 499.99)')
cursor.execute('INSERT INTO products (id, product_name, price) VALUES (3, "Tablet", 299.99)')
cursor.execute('COMMIT')
except Exception as e:
cursor.execute('ROLLBACK')
print(f"An error occurred: {e}")
connection.close()
Output:
3 rows inserted successfully
In this method, we start by creating a table named products
. We then initiate a transaction with BEGIN
and execute multiple insert statements. If all inserts are successful, we commit the transaction. However, if an error occurs during any insert, we roll back the transaction, ensuring that the database remains unchanged. This approach is particularly useful when you want to ensure the atomicity of your database operations.
Using a Single Insert Statement with Multiple Values
If you prefer to keep your code concise, SQLite allows you to insert multiple rows in a single insert statement. This can be particularly useful for inserting a small number of rows and can enhance performance by reducing the number of calls to the database.
import sqlite3
connection = sqlite3.connect('example.db')
cursor = connection.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS orders (id INTEGER PRIMARY KEY, product_id INTEGER, quantity INTEGER)')
cursor.execute('''
INSERT INTO orders (id, product_id, quantity) VALUES
(1, 1, 2),
(2, 2, 5),
(3, 3, 1)
''')
connection.commit()
connection.close()
Output:
3 rows inserted successfully
In this example, we create a table named orders
and then use a single INSERT
statement to add multiple rows. This method is straightforward and efficient for inserting a limited number of records. By combining multiple rows into one statement, we reduce the overhead associated with multiple database calls, which can be beneficial for performance, especially when dealing with larger datasets.
Conclusion
Inserting multiple rows in SQLite is a task that can be accomplished in several ways, each with its own advantages. Whether you choose to use the executemany()
method, wrap your inserts in a transaction, or utilize a single insert statement with multiple values, understanding these techniques can greatly enhance your efficiency when working with databases. As you incorporate these methods into your projects, you’ll find that managing data becomes more streamlined and effective.
FAQ
-
What is the best method to insert multiple rows in SQLite?
The best method depends on your specific use case. For bulk inserts,executemany()
is often preferred, while a single insert statement can be efficient for a small number of rows. -
Can I insert rows without committing the transaction?
No, if you do not commit the transaction, the inserted rows will not be saved to the database. -
Is it possible to insert data from a CSV file into SQLite?
Yes, you can use Python’s built-in CSV module along with SQLite to read data from a CSV file and insert it into a database. -
How can I check if my rows were inserted successfully?
You can execute aSELECT
statement after your insert operation to verify that the data is present in the table. -
What happens if I try to insert duplicate primary keys?
SQLite will raise an error if you attempt to insert a row with a primary key that already exists in the table.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn