SQLite is an open source database and is serverless that needs no configuration. Entire database is a single disk file that can be placed anywhere in operating system's file system. SQLite commands are similar to standard SQL. SQLite is extensively used by applications such as browsers for internal data storage. It is also convenient data storage for embedded devices.
Standard Python library has in built support for SQLite database connectivity. It contains sqlite3 module which is a DB-API V2 compliant module written by Gerhad Haring. It adheres to DB-API 2.0.
The DB-API has been defined in accordance with PEP-249 to ensure similarity between the Python modules that are used to access different databases products.
First step in database handling process is to obtain a connection object representing the database. Next using the connection, obtain a cursor object. A cursor object is similar to the concept of iterator. It is used to process individual rows returned by database queries. A cursor can be viewed as a pointer to one row in a set of rows.
The cursor object helps in executing SQL queries for performing CRUD operations on the underlying database.
Connect() function
This function in sqlite3 module returns connection object representing an existing database on disk , or opens a new database if it doesn't exist already.
import sqlite3 conn=sqlite3.connect('mydb.sqlite3')
SQLite supports creation of in-memory database. Use a special name ':memory:' for that purpose
conn=sqlite3.connect(':memory:')
Following methods are defined in Connection class
cursor() This method returns cursor object.
cur=conn.cursor()
commit() − This method persistently commits transactions to the disk.
rollback() − This method rolls back transactions to restore database state to last call to commit(). This method ensures data consistency in case of exceptions in SQL transactions.
try: conn.commit() except: conn.rollback()
execute() − Implicitly creates cursor and calls its execute() method
executemany() − This method implicitly creates cursor and calls its executemany() method
create_function() − This method creates a user-defined function that you can later use from within SQL statements.
create_aggregate() − This method creates an aggregate function.
iterdump() − This method creates a dump of database in SQL text format.
import sqlite3 con = sqlite3.connect('mydb.sqlite3') f = dump.sql', 'w') for line in con.iterdump(): f.write('%s\n' % line)
backup() − This method creates a backup of SQLite database even while it is being accessed.
source = sqlite3.connect('mydb.sqlite3') dest = sqlite3.connect(':memory:') source.backup(dest)
cursor object
Cursor obtained from connection object facilitates performing various SQL operations on the database using execute() method. For instance, following statement creates a table in the current database
cur = conn.cursor() cur.execute("CREATE TABLE guests ( ID INTEGER PRIMARY KEY, name TEXT (20) NOT NULL, address TEXT (40), city TEXT (20) );"
Following inserts a record in the table
cur.execute('''INSERT INTO GUESTS VALUES(1,'RAJU','ABIDS', 'HYDERABAD');''')
Use executemany() method to perform bulk addition operation
addreses=[(2,'KISHAN', 'TILAK ROAD', 'PUNE'), (3, 'LATA', 'GAANDHI NAGAR', 'AURANGABAD')] sql='INSERT INTO GUESTS VALUES (:ID, :NAME, :ADD, :CITY)' cur.executemany(sql, addreses)
SELECT query forms a result set containing all records returned as a response to query. The execute() method uses a string representing SELECT query statement. There are two methods in DB-API to retrieve rows in the cursor representing the result set.
fetchone() − Fetches next available record from the result set in the form a tuple consisting of values of each column of the fetched record.
fetchall() − Fetches all remaining records in the form of list of tuples. Each tuple corresponds to one row and contains values of each column in the table.
Following code uses fetchone() method to list all records in guests table
sql = "SELECT * from guests;" cur = db.cursor() cur.execute(sql) while True: record=cur.fetchone() if record==None: break print (record)
Use of fetchall() method is demonstrated by following statement
sql = "SELECT * from guests;" cur = db.cursor() cur.execute(sql) set = cur.fetchall() for record in set: print (record)
SQLite and Python types
SQLite data types are by default mapped to equivalent Python data types as per following table
Python type | SQLite type |
---|---|
None | NULL |
int | INTEGER |
float | REAL |
str | TEXT |
bytes | BLOB |
Exceptions
The DB-API defines following exceptions with respect to SQL operations with a SQLite database −
DatabaseError | Exception raised for errors that are related to the database. |
IntegrityError | Exception raised when the relational integrity of the database is affected, e.g. a foreign key check fails. It is a subclass of DatabaseError. |
ProgrammingError | Exception raised for programming errors, e.g.table not found or already exists, syntax error in the SQL statement, the wrong number of parameters specified, etc. |
OperationalError | Exception raised for errors that are related to the database’s operation and not necessarily under the control of the programmer, |
NotSupportedError | Exception raised in case a method or database API was used which is not supported by the database. |