Python Sqlite Tutorial
Python Sqlite Tutorial
i
Python SQLite
This tutorial explains how to communicate with SQLite database in detail, along with
examples.
Audience
This tutorial is designed for python programmers who would like to understand the Python
sqlite3 module in detail.
Prerequisites
Before proceeding with this tutorial, you should have a good understanding of python
programming language. It is also recommended to have basic understanding of the
databases — SQLite.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com
ii
Python SQLite
Table of Contents
About the Tutorial ........................................................................................................................................... ii
Audience .......................................................................................................................................................... ii
Prerequisites .................................................................................................................................................... ii
iii
Python SQLite
iv
1. Python SQLite ― Introduction Python SQLite
SQLite3 can be integrated with Python using sqlite3 module, which was written by Gerhard
Haring. It provides an SQL interface compliant with the DB-API 2.0 specification described
by PEP 249. You do not need to install this module separately because it is shipped by
default along with Python version 2.5.x onwards.
To use sqlite3 module, you must first create a connection object that represents the
database and then optionally you can create a cursor object, which will help you in
executing all the SQL statements.
This API opens a connection to the SQLite database file. You can use ":memory:"
to open a database connection to a database that resides in RAM instead of on
disk. If database is opened successfully, it returns a connection object.
2 connection.cursor([cursorClass])
This routine creates a cursor which will be used throughout your database
programming with Python. This method accepts a single optional parameter
cursorClass. If supplied, this must be a custom cursor class that extends
sqlite3.Cursor.
For example − cursor.execute("insert into people values (?, ?)", (who, age))
This routine is a shortcut of the above execute method provided by the cursor
object and it creates an intermediate cursor object by calling the cursor method,
then calls the cursor's execute method with the parameters given.
1
Python SQLite
5 cursor.executemany(sql, seq_of_parameters)
6 connection.executemany(sql[, parameters])
7 cursor.executescript(sql_script)
This routine executes multiple SQL statements at once provided in the form of
script. It issues a COMMIT statement first, then executes the SQL script it gets
as a parameter. All the SQL statements should be separated by a semi colon
(;).
8 connection.executescript(sql_script)
9 connection.total_changes()
This routine returns the total number of database rows that have been modified,
inserted, or deleted since the database connection was opened.
10 connection.commit()
This method commits the current transaction. If you don't call this method,
anything you did since the last call to commit() is not visible from other database
connections.
11 connection.rollback()
This method rolls back any changes to the database since the last call to
commit().
12 connection.close()
This method closes the database connection. Note that this does not
automatically call commit(). If you just close your database connection without
calling commit() first, your changes will be lost!
13 cursor.fetchone()
This method fetches the next row of a query result set, returning a single
sequence, or None when no more data is available.
2
Python SQLite
14 cursor.fetchmany([size = cursor.arraysize])
This routine fetches the next set of rows of a query result, returning a list. An
empty list is returned when no more rows are available. The method tries to
fetch as many rows as indicated by the size parameter.
15 cursor.fetchall()
This routine fetches all (remaining) rows of a query result, returning a list. An
empty list is returned when no rows are available.
3
2. Python SQLite — Establishing Connection Python SQLite
To establish connection with SQLite Open command prompt, browse through the location
of where you have installed SQLite and just execute the command sqlite3 as shown
below:
To establish a connection with SQLite3 database using python you need to:
The connect() method accepts the name of the database you need to connect with
as a parameter and, returns a Connection object.
Example
import sqlite3
conn = sqlite3.connect('example.db')
Output
print("Connection established ..........")
4
3. Python SQLite ― Create Table Python SQLite
Using the SQLite CREATE TABLE statement you can create a table in a database.
Syntax
Following is the syntax to create a table in SQLite database:
Example
Following SQLite query/statement creates a table with name CRICKETERS in SQLite
database:
Let us create one more table OdiStats describing the One-day cricket statistics of each
player in CRICKETERS table.
5
Python SQLite
);
sqlite>
You can get the list of tables in a database in SQLite database using the .tables command.
After creating a table, if you can verify the list of tables you can observe the newly created
table in it as:
sqlite> . tables
CRICKETERS ODIStats
sqlite>
Create a cursor object by invoking the cursor() method on the above created
connection object.
Now execute the CREATE TABLE statement using the execute() method of the
Cursor class.
Example
Following Python program creates a table named Employee in SQLite3:
import sqlite3
#Connecting to sqlite
conn = sqlite3.connect('example.db')
SEX CHAR(1),
INCOME FLOAT)'''
cursor.execute(sql)
print("Table created successfully........")
Output
Table created successfully........
7
4. Python SQLite — Insert Data Python SQLite
You can add new rows to an existing table of SQLite using the INSERT INTO statement. In
this, you need to specify the name of the table, column names, and values (in the same
order as column names).
Syntax
Following is the recommended syntax of the INSERT statement:
Where, column1, column2, column3,.. are the names of the columns of a table and value1,
value2, value3,... are the values you need to insert into the table.
Example
Assume we have created a table with name CRICKETERS using the CREATE TABLE
statement as shown below:
While inserting records using the INSERT INTO statement, if you skip any columns names,
this record will be inserted leaving empty spaces at columns which you have skipped.
You can also insert records into a table without specifying the column names, if the order
of values you pass is same as their respective column names in the table.
8
Python SQLite
After inserting the records into a table you can verify its contents using the SELECT
statement as shown below:
Create a connection object using the connect() method by passing the name of the
database as a parameter to it.
The cursor() method returns a cursor object using which you can communicate
with SQLite3. Create a cursor object by invoking the cursor() object on the (above
created) Connection object.
Then, invoke the execute() method on the cursor object, by passing an INSERT
statement as a parameter to it.
Example
Following python example inserts records into to a table named EMPLOYEE:
import sqlite3
#Connecting to sqlite
conn = sqlite3.connect('example.db')
print("Records inserted........")
Output
Records inserted........
10
5. Python SQLite ― Select Data Python SQLite
You can retrieve data from an SQLite table using the SELCT query. This query/statement
returns contents of the specified relation (table) in tabular form and it is called as result-
set.
Syntax
Following is the syntax of the SELECT statement in SQLite:
Example
Assume we have created a table with name CRICKETERS using the following query:
Following SELECT query retrieves the values of the columns FIRST_NAME, LAST_NAME
and, COUNTRY from the CRICKETERS table.
11
Python SQLite
Virat|Kohli|India
Rohit|Sharma|India
sqlite>
As you observe, the SELECT statement of the SQLite database just returns the records of
the specified tables. To get a formatted output you need to set the header, and mode
using the respective commands before the SELECT statement as shown below:
sqlite> .header on
sqlite> .mode column
sqlite> SELECT FIRST_NAME, LAST_NAME, COUNTRY FROM CRICKETERS;
First_Name Last_Name Country
---------- -------------------- ----------
Shikhar Dhawan India
Jonathan Trott SouthAfric
Kumara Sangakkara Srilanka
Virat Kohli India
Rohit Sharma India
sqlite>
If you want to retrieve all the columns of each record, you need to replace the names of
the columns with "*" as shown below:
sqlite> .header on
sqlite> .mode column
sqlite> SELECT * FROM CRICKETERS;
First_Name Last_Name Age Place_Of_Birth Country
---------- ---------- ---------- -------------- ----------
Shikhar Dhawan 33 Delhi India
Jonathan Trott 38 CapeTown SouthAfric
Kumara Sangakkara 41 Matale Srilanka
Virat Kohli 30 Delhi India
Rohit Sharma 32 Nagpur India
sqlite>
In SQLite by default the width of the columns is 10 values beyond this width are chopped
(observe the country column of 2nd row in above table). You can set the width of each
column to required value using the .width command, before retrieving the contents of a
table as shown below:
12
Python SQLite
The sqlite3.Cursor class provides three methods namely fetchall(), fetchmany() and,
fetchone() where,
The fetchall() method retrieves all the rows in the result set of a query and returns
them as list of tuples. (If we execute this after retrieving few rows it returns the
remaining ones).
The fetchone() method fetches the next row in the result of a query and returns it
as a tuple.
The fetchmany() method is similar to the fetchone() but, it retrieves the next set
of rows in the result set of a query, instead of a single row.
Note: A result set is an object that is returned when a cursor object is used to query a
table.
Example
Following example fetches all the rows of the EMPLOYEE table using the SELECT query and
from the obtained result set initially, we are retrieving the first row using the fetchone()
method and then fetching the remaining rows using the fetchall() method.
Following Python program shows how to fetch and display records from the COMPANY
table created in the above example.
import sqlite3
#Connecting to sqlite
conn = sqlite3.connect('example.db')
13
Python SQLite
cursor = conn.cursor()
#Retrieving data
cursor.execute('''SELECT * from EMPLOYEE''')
Output
('Ramya', 'Rama priya', 27, 'F', 9000.0)
[('Vinay', 'Battacharya', 20, 'M', 6000.0),
('Sharukh', 'Sheik', 25, 'M', 8300.0),
('Sarmista', 'Sharma', 26, 'F', 10000.0),
('Tripthi', 'Mishra', 24, 'F', 6000.0)]
14
6. Python SQLite — Where Clause Python SQLite
If you want to fetch, delete or, update particular rows of a table in SQLite, you need to
use the where clause to specify condition to filter the rows of the table for the operation.
For example, if you have a SELECT statement with where clause, only the rows which
satisfies the specified condition will be retrieved.
Syntax
Following is the syntax of the WHERE clause in SQLite:
You can specify a search_condition using comparison or logical operators. like >, <, =,
LIKE, NOT, etc. The following examples would make this concept clear.
Example
Assume we have created a table with name CRICKETERS using the following query:
15
Python SQLite
sqlite>
Following SELECT statement retrieves the records whose age is greater than 35:
Create a cursor object by invoking the cursor() method on the above created
connection object.
Now execute the CREATE TABLE statement using the execute() method of the
Cursor class.
Example
Following example creates a table named Employee and populates it. Then using the where
clause it retrieves the records with age value less than 23.
import sqlite3
#Connecting to sqlite
conn = sqlite3.connect('example.db')
16
Python SQLite
AGE INT,
SEX CHAR(1),
INCOME FLOAT)'''
cursor.execute(sql)
print(cursor.fetchall())
Output
[('Vinay', 'Battacharya', 20, 'M', 6000.0)]
17
7. Python SQLite ― Order By Python SQLite
While fetching data using SELECT query, you will get the records in the same order in
which you have inserted them.
You can sort the results in desired order (ascending or descending) using the Order By
clause. By default, this clause sorts results in ascending order, if you need to arrange them
in descending order you need to use “DESC” explicitly.
Syntax
Following is the syntax of the ORDER BY clause in SQLite.
SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
Example
Assume we have created a table with name CRICKETERS using the following query:
18
Python SQLite
sqlite>
Following SELECT statement retrieves the rows of the CRICKETERS table in the ascending
order of their age:
You can use more than one column to sort the records of a table. Following SELECT
statements sorts the records of the CRICKETERS table based on the columns AGE and
FIRST_NAME.
By default, the ORDER BY clause sorts the records of a table in ascending order you can
arrange the results in descending order using DESC as:
19
Python SQLite
Example
In the following example we are creating a table with name and Employee, populating it,
and retrieving its records back in the (ascending) order of their age, using the ORDER BY
clause.
import psycopg2
20
Python SQLite
conn.commit()
print(cursor.fetchall())
Output
[('Vinay', 'Battacharya', 20, 'M', 6000, None),
('Tripthi', 'Mishra', 24, 'F', 6000, None),
('Sharukh', 'Sheik', 25, 'M', 8300, None),
('Sarmista', 'Sharma', 26, 'F', 10000, None),
('Ramya', 'Rama priya', 27, 'F', 9000, None)]
21
8. Python SQLite — Update Table Python SQLite
UPDATE Operation on any database implies modifying the values of one or more records
of a table, which are already available in the database. You can update the values of
existing records in SQLite using the UPDATE statement.
To update specific rows, you need to use the WHERE clause along with it.
Syntax
Following is the syntax of the UPDATE statement in SQLite:
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
Example
Assume we have created a table with name CRICKETERS using the following query:
22
Python SQLite
Following Statement modifies the age of the cricketer, whose first name is Shikhar:
If you retrieve the record whose FIRST_NAME is Shikhar you observe that the age value
has been changed to 45:
If you haven’t used the WHERE clause values of all the records will be updated. Following
UPDATE statement increases the age of all the records in the CRICKETERS table by 1:
If you retrieve the contents of the table using SELECT command, you can see the updated
values as:
Create a connection object using the connect() method by passing the name of the
database as a parameter to it.
The cursor() method returns a cursor object using which you can communicate
with SQLite3 . Create a cursor object by invoking the cursor() object on the (above
created) Connection object.
23
Python SQLite
Then, invoke the execute() method on the cursor object, by passing an UPDATE
statement as a parameter to it.
Example
Following Python example, creates a table with name EMPLOYEE, inserts 5 records into it
and, increases the age of all the male employees by 1:
import sqlite3
#Connecting to sqlite
conn = sqlite3.connect('example.db')
#Inserting data
cursor.execute('''INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME)
VALUES ('Ramya', 'Rama priya', 27, 'F', 9000),('Vinay', 'Battacharya', 20, 'M',
6000), ('Sharukh', 'Sheik', 25, 'M', 8300), ('Sarmista', 'Sharma', 26, 'F',
10000),('Tripthi', 'Mishra', 24, 'F', 6000)''')
conn.commit()
24
Python SQLite
Output
Contents of the Employee table:
[('Ramya', 'Rama priya', 27, 'F', 9000.0), ('Vinay', 'Battacharya', 20, 'M',
6000.0), ('Sharukh', 'Sheik', 25, 'M', 8300.0), ('Sarmista', 'Sharma', 26, 'F',
10000.0), ('Tripthi', 'Mishra', 24, 'F', 6000.0)]
Table updated......
Contents of the Employee table after the update operation:
[('Ramya', 'Rama priya', 27, 'F', 9000.0), ('Vinay', 'Battacharya', 21, 'M',
6000.0), ('Sharukh', 'Sheik', 26, 'M', 8300.0), ('Sarmista', 'Sharma', 26, 'F',
10000.0), ('Tripthi', 'Mishra', 24, 'F', 6000.0)]
25
9. Python SQLite ― Delete Data Python SQLite
To delete records from a SQLite table, you need to use the DELETE FROM statement. To
remove specific records, you need to use WHERE clause along with it.
Syntax
Following is the syntax of the DELETE query in SQLite:
Example
Assume we have created a table with name CRICKETERS using the following query:
Following statement deletes the record of the cricketer whose last name is 'Sangakkara'.
26
Python SQLite
If you retrieve the contents of the table using the SELECT statement, you can see only 4
records since we have deleted one.
If you execute the DELETE FROM statement without the WHERE clause, all the records
from the specified table will be deleted.
Since you have deleted all the records, if you try to retrieve the contents of the
CRICKETERS table, using SELECT statement you will get an empty result set as shown
below:
Create a connection object using the connect() method by passing the name of the
database as a parameter to it.
The cursor() method returns a cursor object using which you can communicate
with SQLite3 . Create a cursor object by invoking the cursor() object on the (above
created) Connection object.
Then, invoke the execute() method on the cursor object, by passing an DELETE
statement as a parameter to it.
Example
27
Python SQLite
Following python example deletes the records from EMPLOYEE table with age value greater
than 25.
import sqlite3
#Connecting to sqlite
conn = sqlite3.connect('example.db')
#Deleting records
cursor.execute('''DELETE FROM EMPLOYEE WHERE AGE > 25''')
Output
Contents of the table:
[('Ramya', 'Rama priya', 27, 'F', 9000.0), ('Vinay', 'Battacharya', 21, 'M',
6000.0), ('Sharukh', 'Sheik', 26, 'M', 8300.0), ('Sarmista', 'Sharma', 26, 'F',
10000.0), ('Tripthi', 'Mishra', 24, 'F', 6000.0)]
Contents of the table after delete operation
[('Vinay', 'Battacharya', 21, 'M', 6000.0), ('Tripthi', 'Mishra', 24, 'F',
6000.0)]
28
10. Python SQLite — Drop Table Python SQLite
You can remove an entire table using the DROP TABLE statement. You just need to specify
the name of the table you need to delete.
Syntax
Following is the syntax of the DROP TABLE statement in PostgreSQL:
Example
Assume we have created two tables with name CRICKETERS and EMPLOYEES using the
following queries:
Now if you verify the list of tables using the .tables command, you can see the above
created tables in it ( list) as:
sqlite> .tables
CRICKETERS EMPLOYEE
sqlite>
Following statement deletes the table named Employee from the database:
Since you have deleted the Employee table, if you retrieve the list of tables again, you can
observe only one table in it.
sqlite> .tables
CRICKETERS
sqlite>
If you try to delete the Employee table again, since you have already deleted it you will
get an error saying “no such table” as shown below:
29
Python SQLite
To resolve this, you can use the IF EXISTS clause along with the DELTE statement. This
removes the table if it exists else skips the DLETE operation.
Example
To drop a table from a SQLite3 database using python invoke the execute() method on
the cursor object and pass the drop statement as a parameter to it.
import sqlite3
#Connecting to sqlite
conn = sqlite3.connect('example.db')
Output
Table dropped...
30
11. Python SQLite ― Limit Python SQLite
While fetching records if you want to limit them by a particular number, you can do so,
using the LIMIT clause of SQLite.
Syntax
Following is the syntax of the LIMIT clause in SQLite:
Example
Assume we have created a table with name CRICKETERS using the following query:
Following statement retrieves the first 3 records of the Cricketers table using the LIMIT
clause:
31
Python SQLite
If you need to limit the records starting from nth record (not 1st), you can do so, using
OFFSET along with LIMIT.
Example
Following python example retrieves the first two records of the EMPLOYEE table using the
LIMIT clause.
import sqlite3
#Connecting to sqlite
conn = sqlite3.connect('example.db')
32
Python SQLite
Output
[('Ramya', 'Rama priya', 27, 'F', 9000.0), ('Vinay', 'Battacharya', 20, 'M',
6000.0), ('Sharukh', 'Sheik', 25, 'M', 8300.0)]
33
12. Python SQLite — Join Python SQLite
When you have divided the data in two tables you can fetch combined records from these
two tables using Joins.
Example
Assume we have created a table with name CRICKETERS using the following query:
Let us create one more table OdiStats describing the One-day cricket statistics of each
player in CRICKETERS table.
Following statement retrieves data combining the values in these two tables:
sqlite> SELECT
34
Python SQLite
import sqlite3
#Connecting to sqlite
conn = sqlite3.connect('example.db')
#Retrieving data
sql = '''SELECT * from EMP INNER JOIN CONTACT ON EMP.CONTACT = CONTACT.ID'''
print(result)
Output
[('Ramya', 'Rama priya', 27, 'F', 9000.0, 101, 101, 'Krishna@mymail.com',
'Hyderabad'), ('Vinay', 'Battacharya', 20, 'M', 6000.0, 102, 102,
35
Python SQLite
36
13. Python SQLite ― Cursor Object Python SQLite
The sqlite3.Cursor class is an instance using which you can invoke methods that execute
SQLite statements, fetch data from the result sets of the queries. You can create Cursor
object using the cursor() method of the Connection object/class.
Example
import sqlite3
#Connecting to sqlite
conn = sqlite3.connect('example.db')
Methods
Following are the various methods provided by the Cursor class/object.
Method Description
fetchone() This method fetches the next row of a query result set,
returning a single sequence, or None when no more data is
available.
fetchmany() This routine fetches the next set of rows of a query result,
returning a list. An empty list is returned when no more rows
are available. The method tries to fetch as many rows as
indicated by the size parameter.
37
Python SQLite
Properties
Following are the properties of the Cursor class:
Method Description
arraySize This is a read/write property you can set the number of rows
returned by the fetchmany() method.
description This is a read only property which returns the list containing the
description of columns in a result-set.
38