Learn to execute the SQLite INSERT Query from Python to add new rows to the SQLite table using a Python sqlite3 module.
Goals of this lesson: –
- Insert single and multiple rows into the SQLite table
- Insert Integer, string, float, double, and
datetimevalues into SQLite table - Use a parameterized query to insert Python variables as dynamic data into a table
Also Read:
Table of contents
Prerequisites
Before executing the following program, please make sure you know the SQLite table name and its column details.
For this lesson, I am using the ‘SqliteDb_developers’ table present in my SQLite database.

If a table is not present in your SQLite database, then please refer to create SQLite table from Python.
Python example to insert a single row into SQLite table
Follow the below steps: –
How to Insert Into SQLite table from Python
- Connect to SQLite from Python
Refer to Python SQLite database connection to connect to SQLite database from Python using sqlite3 module.
- Define a SQL Insert query
Next, prepare a SQL INSERT query to insert a row into a table. in the insert query, we mention column names and their values to insert in a table.
For example,INSERT INTO mysql_table (column1, column2, …) VALUES (value1, value2, …); - Get Cursor Object from Connection
Next, use a
connection.cursor()method to create a cursor object. using cursor object we can execute SQL queries. - Execute the insert query using execute() method
The
cursor.execute(query)method executes the operation stored in the Insert query. - Commit your changes
After successfully executing an insert operation, make changes persistent into a database using the
commit()of a connection class. - Get the number of rows affected
After a successful insert operation, use a
cursor.rowcountmethod to get the number of rows affected. The count depends on how many rows you are Inserting. - Verify result using the SQL SELECT query
If required, execute SQLite select query from Python to see the new changes.
- Close the cursor object and database connection object
use
cursor.clsoe()andconnection.clsoe()method to close the cursor and SQLite connections after your work completes.
As of now, the SqliteDb_developers table is empty, so let’s insert data into it.
Example
Output
Successfully Connected to SQLite Record inserted successfully into table The SQLite connection is closed

Using Python variables in SQLite INSERT query
Sometimes we need to insert a Python variable value into a table’s column. This value can be anything, including integer, string, float, and DateTime. For example, in the registration form person enter his/her details. You can take those values in Python variables and insert them into the SQLite table.
We use a parameterized query to insert Python variables into the table. Using a parameterized query, we can pass python variables as a query parameter in which placeholders (?)
Output:
Connected to SQLite Python Variables inserted successfully into table sqlite connection is closed Connected to SQLite Python Variables inserted successfully into table The SQLite connection is closed

Note: If you have a date column in the SQLite table, and you want to insert the Python DateTime variable into this column then please refer to working with SQLite DateTime values in Python.
Python Insert multiple rows into SQLite table using the cursor’s executemany()
In the above example, we have used execute() method of cursor object to insert a single record. Still, sometimes we need to insert multiple rows into the table in a single insert query.
For example, You wanted to add all records from the CSV file into the SQLite table. Instead of executing the INSERT query every time to add each record, you can perform a bulk insert operation in a single query using a cursor’s executemany() function.
The executemany() method takes two arguments SQL query and records to update.
Output
Connected to SQLite Total 3 Records inserted successfully into table The SQLite connection is closed

verify the result by selecting data from SQLite table from Python.
Let’s understand the above example
- After connecting to SQLite, We prepared a list of records to insert into the SQLite table. Each entry in the list is nothing but a table tuple (row)
- SQL INSERT statement contains the parameterized query, which uses the placeholder (
?) for each column value. - Next, Using
cursor.executemany(sqlite_insert_query, recordList), we inserted multiple rows into the table. - To get to know the number of records inserted, we used a
cursor.rowcountmethod.
Next Steps:
To practice what you learned in this article, Solve a Python Database Exercise project to Practice Database operations.

Very good…
How can i insert values from the command line?
Thanks ,but how to insert multiple rows into ONE column?
from this code iam generating output like this
Processing : 1
sql_column_name : id
bussness_column_name : id
description : test column
gbd_flag : Y
filter_flag : Y
llk_flag : Y
column_width : 5
________________________________________
Processing : 2
sql_column_name : name
bussness_column_name : cl name
description : test column
gbd_flag : Y
filter_flag : Y
llk_flag : Y
column_width : 5
________________________________________
Processing : 3
sql_column_name : site
bussness_column_name : site name
description : test column
gbd_flag : Y
filter_flag : Y
llk_flag : Y
column_width : 15
________________________________________
Processing : 4
sql_column_name : pbm_load_log_key
bussness_column_name : load_log_key
description : test column
gbd_flag : Y
filter_flag : Y
now how i can insert this data into sqlite
Great site ! great examples ! Thanks a lot !.
This works ok in Pycharm :
Python Insert multiple rows into SQLite table using the cursor’s executemany()
but i have the following questions :
1) i want to see the database after the insertions (but it is closed ?)
2) i want to print the database but i donot know howto : can you please explain with an example ?
Thanks again a lot in advance !
Hey Theo,
You can fire the select query immediately after the insertion of multiple rows completed successfully. Please refer to Python Select from SQLite Table
Good work!
How to auto-generate primary key IDs as and when you upload a new file in the DB?
Bcoz there is no point entering values manually when a server is running(if connected). 🙂
Hi Nikilesh,
You need to configure the table in such a way that the primary key gets auto-incremented. either you can use create new table or alter the existing table.
And while inserting data don’t enter this column value in insert query.
for example,
sqlite_insert_blob_query = """ INSERT INTO 'new_employee' ('name', 'photo', 'resume') VALUES (?, ?, ?)"""Referance: https://www.sqlite.org/autoinc.html
just don’t define it at all and use