SBL Python LAB Manual by NY Expt. No. 6
SBL Python LAB Manual by NY Expt. No. 6
AIM: Program to demonstrate CRUD (create, read, update and delete) operations on
databases (SQLite & MySQL) using python.
THEORY:
Sqlite Database
SQLite is a self-contained transactional relational database engine that doesn't require a server
configuration, as in the case of Oracle, MySQL, etc. It is an open source and in-process library
developed by D. Richard Hipp in August 2000. The entire SQLite database is contained in a
single file, which can be put anywhere in the computer's file system.
SQLite is widely used as an embedded database in mobile devices, web browsers and other
stand-alone applications. In spite of being small in size, it is a fully ACID compliant database
conforming to ANSI SQL standards. SQLite is freely downloadable from the official web site.
CRUD Operations
The abbreviation CRUD expands to Create, Read, Update and Delete. These four are the
fundamental operations in a database.
CREATE
The create command is used to create the table in database. First we will go through its
syntax then understand with an example.
Syntax: CREATE TABLE table_name ( Attr1 Type1, Attr2 Type2, … , Attrn Typen ) ;
INSERT
This refers to the insertion of new data into the table. Data is inserted in the form of a
tuple. The number of attributes in the tuple must be equal to that defined in the relation
schema while creating the table.
READ
This refers to reading data from a database. A read statement has three clauses:
SELECT: Takes as the predicate the attributes to be queried, use * for all attributes.
FROM: Takes as the predicate a relation.
WHERE: Takes as the predicate a condition, this is not compulsory.
After executing a read statement in python SQLite3, an iterable cursor object is returned.
This can be used to print data.
UPDATE
This refers to the updating of tuple values already present in the table.
DELETE
As per the prescribed standards, the first step in the process is to obtain the connection to the
object representing the database. In order to establish a connection with a SQLite database,
sqlite3 module needs to be imported and the connect() function needs to be executed.
The connect() function returns a connection object referring to the existing database or a new
database if it doesn't exist.
Method Description
cursor() Returns a Cursor object which uses this Connection.
commit() Explicitly commits any pending transactions to the databse. The method
should be a no-op if the underlying db does not support transactions.
rollback() This optional method causes a transaction to be rolled back to the starting
point. It may not be implemented everywhere.
close() Closes the connection to the database permanently. Attempts to use the
connection after calling this method will raise a DB-API Error.
A cursor is a Python object that enables you to work with the database. It acts as a handle for a
given SQL query; it allows the retrieval of one or more rows of the result. Hence, a cursor object
is obtained from the connection to execute SQL queries using the following statement:
Department of Computer Engineering/SY/SBL-Python/2024-2025
Pg 3
>>> cur=db.cursor()
Method Description
execute() Executes the SQL query in a string parameter
executemany() Executes the SQL query using a set of parameters in the list of tuples
fetchone() Fetches the next row from the query result set.
fetchall() Fetches all remaining rows from the query result set.
callproc() Calls a stored procedure.
close() Closes the cursor object.
import sqlite3
db=sqlite3.connect('test.db')
try:
cur =db.cursor()
cur.execute('''CREATE TABLE student (
StudentID INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT (20) NOT NULL,
age INTEGER,
marks REAL);''')
print ('table created successfully')
except:
print ('error in operation')
db.rollback()
db.close()
Program(Any 1)
CONCLUSION: