0% found this document useful (0 votes)
17 views5 pages

SBL Python LAB Manual by NY Expt. No. 6

This document outlines an experiment aimed at demonstrating CRUD operations (Create, Read, Update, Delete) on SQLite and MySQL databases using Python. It provides theoretical background on SQLite, details the syntax for each CRUD operation, and explains how to establish a database connection and execute SQL queries. Additionally, it includes programming tasks for creating tables and applying CRUD operations in MySQL databases.

Uploaded by

videofor93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

SBL Python LAB Manual by NY Expt. No. 6

This document outlines an experiment aimed at demonstrating CRUD operations (Create, Read, Update, Delete) on SQLite and MySQL databases using Python. It provides theoretical background on SQLite, details the syntax for each CRUD operation, and explains how to establish a database connection and execute SQL queries. Additionally, it includes programming tasks for creating tables and applying CRUD operations in MySQL databases.

Uploaded by

videofor93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT NO.

AIM:​ Program to demonstrate CRUD (create, read, update and delete) operations on
databases (SQLite & MySQL) using python.

SOFTWARE REQUIRED:​ Python IDLE/Jupyter editor/PyCharm/Sqlite & Mysql Database

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.

​ Department of Computer Engineering/SY/SBL-Python/2024-2025


Pg 1
1. To insert attributes in the order specified in the relation schema:​
Syntax: INSERT INTO tableName VALUES ( value1, value2, … valuen )​

2.To insert attributes in the order specified in the relation schema or in a different order:​
INSERT INTO tableName ( Attribute1, Attribute3, Attribute2 . . . ) VALUES ( value1,
value3, value2 . . . )

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.

Example: SELECT NAME, POINTS,


ACCURACY FROM gfg WHERE ACCURACY>85;

UPDATE

​ This refers to the updating of tuple values already present in the table.

Syntax: UPDATE tableName SET Attribute1 = Value1 , Attribute2 = Value2 , . . .​


WHERE condition;​
The WHERE clause must be included, else all records in the table will be updated.

DELETE

​ This refers to the deletion of the tuple present in the table.


​ Department of Computer Engineering/SY/SBL-Python/2024-2025
Pg 2
​ SYNTAX: DELETE FROM tableName WHERE condition
​ If WHERE clause is not used then all the records will be deleted.

​ EXAMPLE: DELETE FROM gfg WHERE ACCURACY>91.

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.

>>> import sqlite3​


>>> db=sqlite3.connect('test.db')

The connect() function returns a connection object referring to the existing database or a new
database if it doesn't exist.

The following methods are defined in the connection class:

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()

The following methods of the cursor object are useful.

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.

Example: Create a New Table in Sqlite

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()

​ Department of Computer Engineering/SY/SBL-Python/2024-2025


Pg 4
__________________________________________________________________________

Program(Any 1)

●​ Create a table Student_Details(Roll_No(Primary key), Name, Class, Division,


Contact_Number, Address) in MySQL Databases using python program and apply
CRUD operations.

●​ Create a table Product_Details(Product_Id(Primary key), Product_Name,


Description, Manufacturer, Price) in MySQL Databases using python program and
apply CRUD operations.

CONCLUSION:

​ Department of Computer Engineering/SY/SBL-Python/2024-2025


Pg 5

You might also like